【发布时间】:2020-06-16 21:07:55
【问题描述】:
我尝试对照片 UIImageView 进行四舍五入如下:
在 viewDidLoad() 中:
photo = UIImageView()
photo.image = UIImage(named: "head")?.withRenderingMode(.alwaysTemplate)
topView.addSubview(photo)
photo.snp.makeConstraints{ make in
make.top.equalToSuperview().offset(gap*3)
make.height.width.equalTo(80)
make.centerX.equalToSuperview()
}
override func viewWillLayoutSubviews() {
photo.makeRounded()
}
extension UIImageView {
func makeRounded() {
self.layer.cornerRadius = self.frame.height / 2
self.layer.borderWidth = 2
self.layer.masksToBounds = false
self.layer.borderColor = UIColor.customyellow.cgColor
self.clipsToBounds = true
}
}
但是当我在模拟器上切换到这个视图时,会出现一个方形框,然后变成一个圆形。有没有办法解决这个问题?谢谢!
编辑:我使用 DispatchQueue 解决了这个问题。感谢另一篇文章的评论。 How to set image in circle in swift
【问题讨论】: