iOS masonry约束立即生效

2021-12-16  本文已影响0人  dalu

在使用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];
上一篇下一篇

猜你喜欢

热点阅读