Autolayout (with Masonry) with corner radius

人盡茶涼 提交于 2019-12-08 03:58:09

问题


I would like to layout a circular UIImageView with Masonry. So I have created the UIImageView like this:

[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);
}];

I then created the constraints for the 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);
}];

I then tried to have these constraints applied and then set the corner radius.

    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
    [self layoutIfNeeded];
    self.imageView.layer.cornerRadius = self.imageView.frame.size.width/2;

But the frame of self.imageView is still unset (but it does get layed out later when I view it on the simulator). What method is called when the layout is done?


回答1:


Setup your corner radius after calling super... in your view's layoutSubviews method:

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

This ensures layout has occurred before attempting to apply corner radii (which in my case is 50% of the height/width to create a circle), and it is calculated correctly.



来源:https://stackoverflow.com/questions/28146330/autolayout-with-masonry-with-corner-radius

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!