问题
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