Ios “与”的区别是什么;setNeedsUpdateConstraints“;及;“是否需要布局?”;?他们什么时候会被叫来?

Ios “与”的区别是什么;setNeedsUpdateConstraints“;及;“是否需要布局?”;?他们什么时候会被叫来?,ios,rotation,autolayout,orientation,nslayoutconstraint,Ios,Rotation,Autolayout,Orientation,Nslayoutconstraint,根据设备方向的不同,autolayout约束有不同的值。我通过以下方式更新约束: - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { [super viewWillTransitionToSize:size withTransitionCoordinator:coordina

根据设备方向的不同,
autolayout
约束有不同的值。我通过以下方式更新约束:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
   [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

   UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

   if ((orientation == UIInterfaceOrientationPortrait) || (orientation == UIInterfaceOrientationPortraitUpsideDown)) {
    self.backgroundView.image = [UIImage imageNamed:@"landscape.jpg"];
    [self updateLandscapeConstraints];
   }
   else if ((orientation == UIInterfaceOrientationLandscapeLeft) ||
            (orientation == UIInterfaceOrientationLandscapeRight)) {
    self.backgroundView.image = [UIImage imageNamed:@"portrait.jpg"];
    [self updatePortraitConstraints];
   }
}

如果更改了某些条件(如偏移或帧),而这些条件会更改约束,请调用setNeedsUpdateConstraints

然后,系统将调用UpdateConstraint作为其正常布局过程的一部分。在需要约束之前立即更新所有约束可确保在布局过程之间对视图进行多次更改时,不会不必要地重新计算约束

如果需要更新约束后的任何操作立即生效,请在其之后使用layoutIfNeeded


因此,您需要首先调用setNeedsUpdateConstraints,一旦调用完毕,您需要调用LayoutIfNeed,因为它会在绘制之前强制子视图的布局,以便更改的约束相应地反映在视图中。

如果您更改了某些条件(如偏移或帧),这些条件将更改您的约束,调用setNeedsUpdateConstraints

然后,系统将调用UpdateConstraint作为其正常布局过程的一部分。在需要约束之前立即更新所有约束可确保在布局过程之间对视图进行多次更改时,不会不必要地重新计算约束

如果需要更新约束后的任何操作立即生效,请在其之后使用layoutIfNeeded


因此,您需要首先调用setNeedsUpdateConstraints,一旦调用完毕,您需要调用LayoutIfNeed,因为它会在绘图之前强制子视图的布局,以便更改的约束相应地反映在视图中。

谢谢。我会根据设备的方向更新大多数约束的
常量
属性,并再次创建一些约束,以调整它们的
乘数
。那么合适调用什么方法呢?还是我两个都打?打电话给LayoutifNeed就足够了。谢谢。我会根据设备的方向更新大多数约束的
常量
属性,并再次创建一些约束,以调整它们的
乘数
。那么合适调用什么方法呢?还是两个都打?打电话给LayoutIfNeed就足够了。
- (void)updateLandscapeConstraints
{
   [self.view layoutIfNeeded];

   self.passwordViewHeight.constant = 34.0;
   self.usernameViewHeight.constant = 34.0;

   [self.view removeConstraint:self.registrationButtonEqualWidth];

   self.registrationButtonEqualWidth = [NSLayoutConstraint constraintWithItem:self.registrationButton
                                                                    attribute:NSLayoutAttributeWidth
                                                                    relatedBy:NSLayoutRelationEqual
                                                                       toItem:self.backgroundView
                                                                    attribute:NSLayoutAttributeWidth
                                                                   multiplier:0.6
                                                                     constant:0.0];

   [self.view addConstraint:self.registrationButtonEqualWidth];

   [self.view layoutIfNeeded];
}