ios - 关于 layoutSubviews 中会不会调用 updateConstraintsIfNeeded

 诸葛二蛋 发布于 2022-10-28 01:38

在调用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,不调用也会更新

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有