Round Top Corners of a UIButton in Swift

前端 未结 10 1845
情深已故
情深已故 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
    

提交回复
热议问题