【问题标题】:make UIImageView round but a square frame appears before being rounded使 UIImageView 变为圆形,但在四舍五入之前会出现一个方形框架
【发布时间】: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

【问题讨论】:

    标签: ios swift uikit swift4


    【解决方案1】:

    在 viewDidLoad() 中而不是在 viewWillLayoutSubviews() 中调用 photo.makeRounded() 方法

    【讨论】:

      【解决方案2】:

      photo = UIImageView().makeRounded()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-31
        • 1970-01-01
        • 1970-01-01
        • 2016-03-27
        • 2021-03-02
        • 2018-12-31
        • 1970-01-01
        • 2023-01-12
        相关资源
        最近更新 更多