Swift中UIButton的圆顶角

时间:2016-05-11 13:16:24

标签: ios swift uibutton rounded-corners

我知道我可以使用以下方式围绕所有四个角:

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

由于我只想进行第二轮,我做了一些研究并找到了this

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

这就是这样称呼的:

view.roundCorners([.TopLeft , .TopRight], radius: 10)

然而,这对UIButton不起作用。当我将扩展名切换为类型UIButton并将其传递给按钮时,输出如下所示:

enter image description here

问题是,如何使其适应UIButton?

10 个答案:

答案 0 :(得分:41)

添加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
    }
}

在viewDidAppear / viewDidLayoutSubviews中调用

btnCorner.roundedButton()

按钮角OutPut:

enter image description here

答案 1 :(得分:23)

Swift 4:最新的iOS 11以后

override func viewDidLoad() {
    super.viewDidLoad()

    if #available(iOS 11.0, *) {
        self.viewToRound.clipsToBounds = true
        viewToRound.layer.cornerRadius = 20
        viewToRound.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
    } else {
        // Fallback on earlier versions
    }
}

较早的iOS(10,9等)版本(适用于iOS 11)

override func viewDidLayoutSubviews() {
    self.viewToRound.clipsToBounds = true
    let path = UIBezierPath(roundedRect: viewToRound.bounds,
                            byRoundingCorners: [.topRight, .topLeft],
                            cornerRadii: CGSize(width: 20, height: 20))

    let maskLayer = CAShapeLayer()

    maskLayer.path = path.cgPath
    viewToRound.layer.mask = maskLayer
}

答案 2 :(得分:21)

对于 swift 3 Kirit Modi的回答改为:

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

在扩展文件开头,请不要忘记添加:

import UIKit

如果你想要一个 UIView 的扩展名,你可以使用圆角顶角或底角的选项:

extension UIView {
   func roundedCorners(top: Bool){
       let corners:UIRectCorner = (top ? [.topLeft , .topRight] : [.bottomRight , .bottomLeft])        
       let maskPAth1 = UIBezierPath(roundedRect: self.bounds,
                                    byRoundingCorners: corners,
                                    cornerRadii:CGSize(width:8.0, height:8.0))
       let maskLayer1 = CAShapeLayer()
       maskLayer1.frame = self.bounds
       maskLayer1.path = maskPAth1.cgPath
       self.layer.mask = maskLayer1
   }
}

按钮的名称为:

myButton.roundedCorners(top: true)

答案 3 :(得分:6)

iOS 11让圆角变得非常容易。下面的代码围绕左上角和右下角。

myView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMaxYCorner]

答案 4 :(得分:5)

将您的扩展程序更新为:

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

形状图层(蒙版)需要知道框架

答案 5 :(得分:1)

您忘了设置形状图层的框架:

mask.frame = layer.bounds

答案 6 :(得分:0)

使用此代码,

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

希望它有用

答案 7 :(得分:0)

对于 swift 5 和最灵活的操作

使用roundCorners函数定义扩展名

extension UIButton {
    func roundCorners(corners: UIRectCorner, radius: Int = 8) {
        let maskPath1 = UIBezierPath(roundedRect: bounds,
                                     byRoundingCorners: corners,
                                     cornerRadii: CGSize(width: radius, height: radius))
        let maskLayer1 = CAShapeLayer()
        maskLayer1.frame = bounds
        maskLayer1.path = maskPath1.cgPath
        layer.mask = maskLayer1
    }
}

调用roundCorners函数

myButton.roundCorners(corners: [.topLeft, .topRight])

enter image description here

或具有特定半径

myButton.roundCorners(corners: [.topLeft, .topRight], radius: 20)

enter image description here

答案 8 :(得分:0)

四舍五入应用于视图/按钮..的角,但是到达按钮的边框时,它不能正确应用。我有什么解决办法吗? @

这是我使用的代码,它在iOS11.0及更高版本中有效(边界),而在低于版本(<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

答案 9 :(得分:0)

那对我有帮助

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
   }
}

请注意以下事实:如果您附加了布局约束,则必须在UIView子类中按如下所示刷新它:

override func viewDidLayoutSubviews() {
   super.viewDidLayoutSubviews()

   yourButtonOutletName.roundCorners(
      corners: [.topLeft, .topRight],
      radius: yourButtonOutletName.frame.height / 2
   )
}