Button Border with Transparent Background in Swift

前端 未结 4 2108
灰色年华
灰色年华 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:10

    using Extension:

    extension UIButton
    {
     func setUpLayer(sampleButton: UIButton?) {
      sampleButton!.setTitle("GET STARTED", forState: UIControlState.Normal)
      sampleButton?.tintColor =  UIColor.whiteColor()
      sampleButton!.frame = CGRect(x:50, y:500, width:170, height:40)
      sampleButton!.layer.borderWidth = 1.0
      sampleButton!.layer.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.5).CGColor
      sampleButton!.layer.cornerRadius = 5.0
    
      sampleButton!.layer.shadowRadius =  3.0
      sampleButton!.layer.shadowColor =  UIColor.whiteColor().CGColor
      sampleButton!.layer.shadowOpacity =  0.3
     }
    
    }
    

    Usage:

      let sampleUIButton =  UIButton()
      sampleUIButton.setUpLayer(sampleUIButton)
      self.view.addSubview(sampleUIButton)
    

提交回复
热议问题