我正在尝试在tableview单元格中绘制圆形图像。我已经想出了当图像处于普通视图时如何绘制圆圈:我将此代码放在viewDidLayoutSubviews()中,以便仅在自动布局完成后才将我的更改应用于图像。
profilePicture2.layer.cornerRadius = profilePicture2.frame.size.height/2
profilePicture2.clipsToBounds = true
我从这个答案中得到了一个提示:Why does adding an align with centre constraint to a UIImage throw out the radius of corners setting?
但是,如果图像位于动态tableview单元格中,我该怎么做?有没有办法对单个单元格使用viewDidLayoutSubviews(),所以我可以在自动布局完成后设置我的图像的圆角半径?
提前致谢。
答案 0 :(得分:2)
您可以为profilePicture创建一个自定义类,该类扩展UIView并覆盖layoutSubviews
。
override func layoutSubviews() {
layer.cornerRadius = frame.size.height/2
}