iOS masonry约束立即生效

在使用masonry设置布局时,设置的约束并不会立即生效,此时获取frame都为0。但有的时候,我们需要获取此时的宽高等,这时就需要设置控件的约束立即生效。

其实很简单:用它的父视图调用layoutIfNeeded就可以立即生效。

例如:
self.briefBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[self.contentView addSubview:self.briefBtn];
[self.briefBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.mas_equalTo(self.contentView);
    make.top.mas_equalTo(self.checkBtn.mas_bottom);
    make.width.mas_equalTo(40);
    make.height.mas_equalTo(45);
}];

 //只有父视图调用了 layoutIfNeeded,后面ipTextField的frame设置才会生效
[self.contentView layoutIfNeeded];

self.ipTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.checkBtn.frame), CGRectGetMaxY(self.checkBtn.frame), dl_screenWidth()-CGRectGetWidth(self.checkBtn.frame)-CGRectGetWidth(self.briefBtn.frame), CGRectGetHeight(self.checkBtn.frame))];
[self.contentView addSubview:self.ipTextField];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容