【问题标题】:Transparent background button with 2 sided corner radius in SwiftSwift中具有2边角半径的透明背景按钮
【发布时间】:2020-10-30 07:07:19
【问题描述】:

我正在构建自己的分段控制。它是一个空心(透明)的分段控件。选定段的边界宽度被强调。每个段都是水平堆栈中的一个按钮。这就是我想要实现的目标:

我正在使用这个扩展来圆选定的按钮

extension UIButton {
    func roundedButton(corners: UIRectCorner, radius: CGFloat){
        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
    }
}

输出如下:

无论如何只在左边圆角而不用遮罩?

【问题讨论】:

    标签: swift uibutton uibezierpath rounded-corners cornerradius


    【解决方案1】:

    您可以像下面这样编辑roundCorners函数;

    extension UIButton {
        
        func roundCorners(_ corners: UIRectCorner, radius: CGFloat, borderColor: UIColor, borderWidth: CGFloat) {
            let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
            
            let mask = CAShapeLayer()
            mask.path = path.cgPath
            layer.mask = mask
            
            let borderLayer = CAShapeLayer()
            borderLayer.path = path.cgPath
            borderLayer.lineWidth = borderWidth
            borderLayer.strokeColor = borderColor.cgColor
            borderLayer.fillColor = UIColor.clear.cgColor
            borderLayer.frame = bounds
            layer.addSublayer(borderLayer)
        }
    }
    

    并像这样使用它:

    let button = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 40))
    button.setTitle("Today", for: .normal)
    button.roundCorners([.topLeft, .bottomLeft], radius: 25, borderColor: .white, borderWidth: 10)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      • 2020-03-14
      • 2021-10-02
      • 1970-01-01
      • 2011-09-15
      • 2014-02-13
      • 2015-05-18
      相关资源
      最近更新 更多