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

Getting "Unable to simultaneously satisfy constraints." warning when updating height constraint at rotation

我需要在旋转设备时更改视图的高度。我将 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:

中得到了这个日志

Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)

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

Will attempt to recover by breaking constraint id: login.1, constant: 40.000000

我想替换约束以在设备方向发生变化时为其提供新常量,我做错了什么?

提前致谢

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

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

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