Button Border with Transparent Background in Swift

前端 未结 4 2094
灰色年华
灰色年华 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 05:55

    Setting the backgroundColor to clearColor makes the button transparent.
    Try the code below for example. You can configure and vary the borderAlpha,cornerRadius and colours as your want.

    let borderAlpha : CGFloat = 0.7
    let cornerRadius : CGFloat = 5.0
    
    button.frame = CGRectMake(100, 100, 200, 40)
    button.setTitle("Get Started", forState: UIControlState.Normal)
    button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
    button.backgroundColor = UIColor.clearColor()
    button.layer.borderWidth = 1.0
    button.layer.borderColor = UIColor(white: 1.0, alpha: borderAlpha).CGColor
    button.layer.cornerRadius = cornerRadius
    

提交回复
热议问题