eli*_*lon 14 cornerradius contentmode ios autolayout
我有一个UIImageView配置了自动布局的对象.我创建了约束,以便视图与其superview保持恒定的距离.在视觉格式中它将是:
@"V:|-[imageView]-|"
@"H:|-[imageView]-|"
Run Code Online (Sandbox Code Playgroud)
但我还想保留底层图像的宽高比,所以我已经分配UIViewContentModeScaleAspectFit给了contentMode.
我认为一切都运行良好,直到我设置相关的cornerRadius值CALayer:
self.imageView.layer.cornerRadius = 7;
self.imageView.layer.maskToBounds = YES;
Run Code Online (Sandbox Code Playgroud)
现在,当调整图像视图的大小时,例如由于方向的改变,圆角会根据视图获得的新大小而丢失.原因是cornerRadius适用于UIImageView(下面的破折号中的框架),但由于底层图像也调整为尊重contentMode(下面的星号中的框架),因此圆角不再可见:
--------------------------
| ********** |
| * * |
| * * |
| * * |
| ********** |
--------------------------
Run Code Online (Sandbox Code Playgroud)
有没有办法防止这种行为?
noi*_*gle 27
比如@ jacob -k说,你应该添加到你的单元子类layoutSubviews,但只是添加你应该layoutIfNeeded先调用.
例:
- (void)layoutSubviews
{
[super layoutSubviews];
[self layoutIfNeeded];
imageView.layer.cornerRadius = imageView.frame.size.width / 2;
}
Run Code Online (Sandbox Code Playgroud)
如果您不layoutIfNeeded先调用,则它会应用所有更改而不受您设置的约束.