How to keep a round imageView round using auto layout?

后端 未结 15 1373
悲&欢浪女
悲&欢浪女 2020-12-01 06:22

How do I turn a rectangular image view into a circular image view that can hold shape in auto layout without setting width and height restraints? Thereby allowing the imageV

15条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-01 06:44

    I added custom IBInspectable cornerRadiusPercent, so you can do it without any code.

    class RoundButton: UIButton {
    
        override var bounds: CGRect {
            didSet {
                updateCornerRadius()
            }
        }
    
    
        //private var cornerRadiusWatcher : CornerRadiusPercent?
        @IBInspectable var cornerRadiusPercent: CGFloat = 0 {
    
            didSet {
                updateCornerRadius()
            }
        }
    
        func updateCornerRadius()
        {
            layer.cornerRadius = bounds.height * cornerRadiusPercent
        }
    
    }
    

提交回复
热议问题