How to make a simple rounded button in Storyboard?

后端 未结 13 1844
渐次进展
渐次进展 2021-01-29 19:49

I just started learning iOS development, cannot find how to make simple rounded button. I find resources for old versions. Do I need to set a custom background for a button? In

13条回答
  •  攒了一身酷
    2021-01-29 20:00

    You can do something like this:

    @IBDesignable class MyButton: UIButton
    {
        override func layoutSubviews() {
            super.layoutSubviews()
    
            updateCornerRadius()
        }
    
        @IBInspectable var rounded: Bool = false {
            didSet {
                updateCornerRadius()
            }
        }
    
        func updateCornerRadius() {
            layer.cornerRadius = rounded ? frame.size.height / 2 : 0
        }
    }
    

    Set class to MyButton in Identity Inspector and in IB you will have rounded property:

提交回复
热议问题