如何以编程方式更改或更新NSLayoutConstraint

我已经使用代码以编程方式实现了AutoLayout:

- (void)addConstraintWithListNavigationViewController:(UIView *)listViewNavigation y:(CGFloat)y height:(CGFloat)height { //WIDTH_ListTableView = 0.4 //set x = 0; NSLayoutConstraint *constraintToAnimate1 = [NSLayoutConstraint constraintWithItem:listViewNavigation attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:0.00 constant:0]; [self.view addConstraint:constraintToAnimate1]; //set y = y; NSLayoutConstraint *constraintToAnimate2 = [NSLayoutConstraint constraintWithItem:listViewNavigation attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:0.00 constant:y]; [self.view addConstraint:constraintToAnimate2]; //set Width = self.view.frame.size.width*0.4 NSLayoutConstraint *constraintToAnimate3 = [NSLayoutConstraint constraintWithItem:listViewNavigation attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1-WIDTH_ListTableView constant:0.0]; [self.view addConstraint:constraintToAnimate3]; //Set height = height NSLayoutConstraint *constraintToAnimate4 = [NSLayoutConstraint constraintWithItem:listViewNavigation attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.00 constant:height]; [self.view addConstraint:constraintToAnimate4]; } 

这工作完美,但每次这个ViewController收到一个通知 ,它会运行:

 [self.view layoutIfNeeded]; 

但我想根据连接的布尔variables设置listViewNavigation的宽度。

 if(connected){ listViewNavigation.view.frame.size.width = 0.4 * self.view.frame.size.width; } else{ listViewNavigation.view.frame.size.width = 0.6 * self.view.frame.size.width; } 

但我不知道如何更新NSLayoutConstraint:

 NSLayoutConstraint *constraintToAnimate3 = [NSLayoutConstraint constraintWithItem:PreView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 - WIDTH_ListTableView constant:0.0]; [self.view addConstraint:constraintToAnimate3]; 

当这个ViewController收到通知

我想你有两个select。

选项1保留一个属性

 @property (strong,nonatomic)NSLayoutConstraint *constraintToAnimate3; 

然后使用这个属性来

 self.constraintToAnimate3 = [NSLayoutConstraint constraintWithItem:PreView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 constant:-1 * 0.4 * self.view.frame.size.width]; [self.view addConstraint:self.constraintToAnimate3]; 

当你想改变

 if(connected){ self.constraintToAnimate3.constant = -1 *0.6 * self.view.frame.size.width; } else{ self.constraintToAnimate3.constant = -1 *0.4 * self.view.frame.size.width; } [UIView animateWithDuration:yourduration animations:^{ [self.view layoutIfNeeded]; }]; 

选项2设置constraintToAnimate3的标识符

 constraintToAnimate3.identifier = @"1234" 

然后search以获得约束

 NSLayoutConstraint * constraint3 = nil; NSArray * constraints = self.view.constraints; for (NSLayoutConstraint * constraint in constraints) { if ([constraint.identifier isEqualToString:@"1234"]) { constraint3 = constraint; break; } } 

然后改变常量,如Option1所示

更新:如果在我发布的代码中使用常数

PreView.frame.size.with = self.view.size.width *乘数+常量

好的,我知道了。

 [self.view removeConstraint:self.constraintOld]; [self.view addConstraint:self.constraintNew]; [UIView animateWithDuration:time animations:^{ [self.view layoutIfNeeded]; }]; 

不要只是将约束添加到视图中,而是要记住它们。

如果你只需要改变它的常量,那么改变一个约束是最容易的 – 常数是约束的唯一部分,可以在以后重复改变。 要做到这一点,您需要自行存储约束。

否则,您需要删除旧约束并添加新约束。 由于您通常有多个约束,因此需要将需要replace的约束集存储到数组中,然后更新整个数组。 您也可以使用activateConstraints和deactivateConstraints方法。