我想用Masonry布局一个圆形的UIImageView。所以我创建了像这样的UIImageView:
[self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.mas_centerX);
make.top.equalTo(self.mas_top).with.offset([[UIView ub_padding] floatValue]);
make.bottom.equalTo(self.descriptionLabel.mas_top).with.offset(-[[UIView ub_padding] floatValue]);
make.height.equalTo(self.descriptionLabel.mas_height).priorityHigh();
make.width.equalTo(self.imageView.mas_height);
}];
然后我为ImageView
创建了约束[self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.mas_centerX);
make.top.equalTo(self.mas_top).with.offset([[UIView ub_padding] floatValue]);
make.bottom.equalTo(self.descriptionLabel.mas_top).with.offset(-[[UIView ub_padding] floatValue]);
make.height.equalTo(self.descriptionLabel.mas_height).priorityHigh();
make.width.equalTo(self.imageView.mas_height);
}];
然后我尝试应用这些约束,然后设置角半径。
[self setNeedsUpdateConstraints];
[self updateConstraintsIfNeeded];
[self layoutIfNeeded];
self.imageView.layer.cornerRadius = self.imageView.frame.size.width/2;
但是self.imageView
的框架仍未设置(但是当我在模拟器上查看它时,它确实会被放置)。布局完成后调用什么方法?
答案 0 :(得分:1)
在视图的super...
方法中调用layoutSubviews
后设置您的角半径:
-(void)layoutSubviews{
[super layoutSubviews];
[self applyCornerRadii];
}
这确保在尝试应用角半径之前已经发生布局(在我的情况下是高度/宽度的50%来创建圆),并且它是正确计算的。