在旋转时更新高度约束时收到"Unable to simultaneously satisfy constraints."警告



旋转设备时,我需要更改视图的高度。我使用storyboardautolayout,并且在视图控制器中将高度约束设置为IBOutlet

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *passwordViewHeight;

然后,在视图控制器中我也有以下方法:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
   [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
    {
        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
        [self.view setNeedsUpdateConstraints];
    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
    {
    }];
   [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
- (void)updateViewConstraints
{
   [super updateViewConstraints];
   UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
   if (orientation == UIInterfaceOrientationPortrait) {
       [self.welcomeLabel setHidden:NO];
       [self setAllPortraitConstraints];
   }
   else if ((orientation == UIInterfaceOrientationLandscapeLeft) ||
            (orientation == UIInterfaceOrientationLandscapeRight)) {
       [self.welcomeLabel setHidden:YES];
       [self setAllLandscapeConstraints];
   }
}
- (void)setAllLandscapeConstraints
{
   [self.view removeConstraint:self.passwordViewHeight];
   // More constraints
   self.passwordViewHeight = [NSLayoutConstraint constraintWithItem:self.passwordView
                                                          attribute:NSLayoutAttributeHeight
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute:NSLayoutAttributeNotAnAttribute
                                                         multiplier:1.0
                                                           constant:34.0];
   // More constraints
   [self.view addConstraint:self.passwordViewHeight];
   // More constraints
}
- (void)setAllPortraitConstraints
{
   [self.view removeConstraint:self.passwordViewHeight];
   // More constraints
   self.passwordViewHeight = [NSLayoutConstraint constraintWithItem:self.passwordView
                                                          attribute:NSLayoutAttributeHeight
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute:NSLayoutAttributeNotAnAttribute
                                                         multiplier:1.0
                                                           constant:40.0];
   // More constraints
   [self.view addConstraint:self.passwordViewHeight];
   // More constraints
}

然后,当我以纵向方向运行应用程序,并将设备旋转到横向时,我在Xcode:中得到了这个日志

无法同时满足约束。以下列表中可能至少有一个约束是您不想要的。试着这样做:(1)看看每一个约束,试着找出你没有想到的;(2) 找到添加了不需要的一个或多个约束的代码并修复它。(注意:如果您看到的是不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性translatesAutoresizingMaskIntoConstraints的文档)

(
   "id: login.1, constant: 40.000000",
   "id: (null), constant: 34.000000"
)

将尝试通过打破约束来恢复id:login.1,常量:40.000000

当设备方向发生变化时,我想用约束来替换它,给它一个新的常量,我做错了什么?

提前感谢

尝试在loadView或某些初始化代码中创建约束。然后将代码更改为:试试这个:

- (void)setAllLandscapeConstraints
{
   self.passwordViewHeight.constant = 34;
}
- (void)setAllPortraitConstraints
{
   self.passwordViewHeight.constant = 40;
}

相关内容

最新更新