在调用updateConstraintsIfNeeded可能会立即执行updateConstraints然后调用layoutSubviews,但是在layoutSubviews的时候会不会调用updateConstraintsIfNeeded呢?
比如说在viewDidLoad中初始化并布局了view,然后在viewDidAppear的时候更新了约束,这时候如果调用layoutIfNeeded,在layoutSubviews中会不会调用updateConstraintsIfNeeded呢?以下是一部分代码做个示范
- (void)viewDidLoad {
[super viewDidLoad];
self.cview = [[CustomView alloc] init];
[self.view addSubview:self.cview];
[NSLayoutConstraint constraintWithItem:view4 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:8.0].active = YES;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[NSLayoutConstraint constraintWithItem:view4 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0].active = YES;
[self layoutIfNeeded];
}
网上有人说会,但是我自己试了下貌似并没有。
但是按照Autolayout布局的步骤,应该是先更新约束然后更新布局,在上面的例子中layoutIfNeeded直接调用了layoutSubviews,照理来说也应该去询问下是不是需要更新约束吧???
另外上面的代码中是不是不需要调用layoutIfNeeded,不调用也会更新
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
认证0级讲师