获取“无法同时满足约束”。在更新高度约束时发出警告

问题描述:

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

无法同时sa脆弱的约束。 以下列表中的至少一个约束可能是您不想要的。试试这个:(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; 
} 
+0

约束在故事板创建,使所有需要的是你所示的代码。 – Paulw11

+0

谢谢。 'updateViewConstraints'是进行更改的正确位置吗?我在其他地方发现了一些其他的帖子,例如'viewWillTransitionToSize:'...我不明白与自动布局有关的布局,约束和旋转的方法... – AppsDev