Round Top Corners of a UIButton in Swift

前端 未结 10 1847
情深已故
情深已故 2020-11-27 16:33

I know I can round all four corners using:

 myBtn.layer.cornerRadius = 8
 myBtn.layer.masksToBounds = true

Since I only want to round two,

相关标签:
10条回答
  • 2020-11-27 17:13

    rounding is applied to corner's of view/Button .. , but coming to border of button , it is not applying correctly. can I have any solution for that? @

    Here is the code that I have used , which is working (border) in iOS11.0 and above and not in below versions(<11.0)

    if #available(iOS 11.0, *) {
            self.layer.cornerRadius = radius
            self.layer.maskedCorners = maskedCorners
        } else {
            let shapeLayer = CAShapeLayer()
            shapeLayer.position = self.center
            self.layer.masksToBounds = true
            self.clipsToBounds = true
            let bezirePath = UIBezierPath(roundedRect: self.bounds,
                                          byRoundingCorners: corners,
                                          cornerRadii: CGSize(width: radius, height: radius))
            shapeLayer.bounds = frame
            shapeLayer.path = bezirePath.cgPath
    
            self.layer.mask = shapeLayer
    
    0 讨论(0)
  • 2020-11-27 17:15

    Adding Extension of UIButton:

    extension UIButton{
        func roundedButton(){
            let maskPath1 = UIBezierPath(roundedRect: bounds,
                byRoundingCorners: [.topLeft , .topRight],
                cornerRadii: CGSize(width: 8, height: 8))
            let maskLayer1 = CAShapeLayer()
            maskLayer1.frame = bounds
            maskLayer1.path = maskPath1.cgPath
            layer.mask = maskLayer1
        }
    }
    

    Calling in viewDidAppear/viewDidLayoutSubviews:

    btnCorner.roundedButton()
    

    Button Corner OutPut:

    0 讨论(0)
  • 2020-11-27 17:15

    Use this Code,

    let path = UIBezierPath(roundedRect:viewTo.bounds, byRoundingCorners:[.TopRight, .TopLeft], cornerRadii: CGSizeMake(20, 20))
    let maskLayer = CAShapeLayer()
    maskLayer.path = path.CGPath
    viewTo.layer.mask = maskLayer
    

    hope its helpful

    0 讨论(0)
  • 2020-11-27 17:16

    That's what helped me

    extension UIView {
    
       func roundCorners(
          corners: UIRectCorner, 
          radius: CGFloat
       ) {
          let path = UIBezierPath(
             roundedRect: bounds,
             byRoundingCorners: corners,
             cornerRadii: CGSize(
                width: radius, 
                height: radius
             )
          )
    
          let mask = CAShapeLayer()
          mask.path = path.cgPath
          layer.mask = mask
       }
    }
    

    Pay attention to the fact that if you have layout constraints attached to it, you must refresh this as follows in your UIView subclass:

    override func viewDidLayoutSubviews() {
       super.viewDidLayoutSubviews()
    
       yourButtonOutletName.roundCorners(
          corners: [.topLeft, .topRight],
          radius: yourButtonOutletName.frame.height / 2
       )
    }
    
    0 讨论(0)
提交回复
热议问题