【问题标题】:How to cut of corners of UIButton, not round them?如何切掉 UIButton 的角,而不是圆角?
【发布时间】:2019-10-04 16:18:39
【问题描述】:

我想快速切掉 UIButton 的边角。

我知道如何设置拐角半径,但我想剪掉拐角。

// what I DON'T want, rounded corners
myButton.layer.cornerRadius = myButton.frame.height / 2

切角是给我的:

【问题讨论】:

标签: swift uibutton


【解决方案1】:

用这样的所需路径创建一个 CAShapeLayer

let button = UIButton()
button.backgroundColor = .red
button.frame = CGRect(x: 100, y: 100, width: 200, height: 40)
view.addSubview(button)

let layer = CAShapeLayer()
layer.fillColor = UIColor.yellow.cgColor

let cutSize:CGFloat = 20
let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: button.bounds.midY))
path.addLine(to: CGPoint(x: cutSize, y: 0))
path.addLine(to: CGPoint(x: button.bounds.maxX-cutSize, y: 0))
path.addLine(to: CGPoint(x: button.bounds.maxX, y: button.bounds.midY))
path.addLine(to: CGPoint(x: button.bounds.maxX-cutSize, y: button.bounds.maxY))
path.addLine(to: CGPoint(x: cutSize, y: button.bounds.maxY))
path.addLine(to: CGPoint(x: 0, y: button.bounds.midY))

layer.path = path.cgPath
button.layer.insertSublayer(layer, at: 0)

【讨论】:

    【解决方案2】:

    您可以通过以下方式实现:

    sampleButton.layer.cornerRadius = 15
    sampleButton.clipsToBounds = true
    

    【讨论】:

    • 这与我的问题中的代码 sn-p 有何不同?我不想要圆角,而是切角。
    • 切角和圆角有什么区别?
    • 这就像用刀切按钮的角
    • @swiftlynx 不可行,可以设置按钮图片。
    猜你喜欢
    • 2016-04-20
    • 2016-04-29
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    相关资源
    最近更新 更多