image disappears when styling class to make a round image

前端 未结 2 693
执笔经年
执笔经年 2021-01-21 13:54
class RoundImage: UIImageView {

    override func awakeFromNib() {
        super.awakeFromNib()
        setupView()
    }

     ...

    func setupView() {
        self         


        
相关标签:
2条回答
  • 2021-01-21 13:59

    When this code gets run the view hasn't been sized yet, so the width is incorrect. Try putting a listener on frame:

    override var frame : CGRect {
        didSet {
            layer.cornerRadius = frame.size.width / 2
        }
    }
    
    0 讨论(0)
  • 2021-01-21 14:08

    I faced with this problem a few days ago. You should add layoutIfNeeded() before layer modifications.

    In your case:

    func setupView() {
            self.layoutIfNeeded()
            self.layer.borderWidth = borderWidth
            self.layer.borderColor = borderColor
            self.clipsToBounds = clip
            self.layer.cornerRadius = self.frame.size.width / 2
     }
    
    0 讨论(0)
提交回复
热议问题