具有拐角半径的自动布局(使用砌体) [英] Autolayout (with Masonry) with corner radius

查看:20
本文介绍了具有拐角半径的自动布局(使用砌体)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 Masonry 布局一个圆形的 UIImageView.所以我像这样创建了 UIImageView:

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

然后我为 ImageView 创建了约束

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;

但是 self.imageView 的框架仍然没有设置(但是当我稍后在模拟器上查看它时它确实得到了布局).布局完成后调用什么方法?

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?

推荐答案

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

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

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

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

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.

这篇关于具有拐角半径的自动布局(使用砌体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆