Button Border with Transparent Background in Swift

前端 未结 4 2106
灰色年华
灰色年华 2021-02-04 05:37

How can I make a UIButton border to look alike in the below image (the \"Getting Started\") button with a transparent background?

How should I achieve this

4条回答
  •  星月不相逢
    2021-02-04 06:02

    Using Swift 3, for Storyboard, just add this subclass to your project, then in the Identity Inspector, make this the class for the UIButton on your storyboard. You should then be able to adjust the boarder color and width.

    @IBDesignable class CustomButton: UIButton {
    
     @IBInspectable var borderColor: UIColor = UIColor.white {
        didSet {
            layer.borderColor = borderColor.cgColor
        }
     }
    
     @IBInspectable var borderWidth: CGFloat = 2.0 {
        didSet {
            layer.borderWidth = borderWidth
        }
     }
    
     override public func layoutSubviews() {
        super.layoutSubviews()
        clipsToBounds = true
     }
    }
    

提交回复
热议问题