带有角半径的Autolayout(带有Masonry)

时间:2015-01-26 07:50:49

标签: ios objective-c uiimageview

我想用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的框架仍未设置(但是当我在模拟器上查看它时,它确实会被放置)。布局完成后调用什么方法?

1 个答案:

答案 0 :(得分:1)

在视图的super...方法中调用layoutSubviews后设置您的角半径:

-(void)layoutSubviews{
    [super layoutSubviews];
    [self applyCornerRadii];
}

这确保在尝试应用角半径之前已经发生布局(在我的情况下是高度/宽度的50%来创建圆),并且它是正确计算的。