将拐角半径设置为UIView / UIButton的特定边

时间:2016-10-21 06:27:41

标签: ios uiview uibutton swift3 cornerradius

我想将角半径设置为我的UI元素,如附加的snap:

所示

enter image description here

我尝试使用UIBezierPath设置转角半径,但它没有给出预期的结果。我还在UIButton中添加了这三个UIView并将角半径设置为容器视图但没有用。

参考https://stackoverflow.com/a/31233171/4886069,以下是我在顶部和底部按钮上尝试过的代码:

let rectShape = CAShapeLayer()
rectShape.bounds = self.scanButton.frame
rectShape.position = self.scanButton.center
rectShape.path = UIBezierPath(roundedRect: self.scanButton.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 20, height: 20)).cgPath
self.scanButton.layer.mask = rectShape

let rectShape1 = CAShapeLayer()
rectShape1.bounds = self.manualButton.frame
rectShape1.position = self.manualButton.center
rectShape.path = UIBezierPath(roundedRect: self.manualButton.bounds, byRoundingCorners: [.bottomLeft, .bottomRight], cornerRadii: CGSize(width: 20, height: 20)).cgPath
self.manualButton.layer.mask = rectShape1

以下是我在容器视图上尝试过的代码:

containerView.layer.cornerRadius = 20

以下是我得到的输出:

enter image description here

任何帮助表示赞赏。谢谢

3 个答案:

答案 0 :(得分:0)

byRoundingCorners:[.topLeft] 选项中使用topRight,bottomLeft和bottomRight作为预期结果的其他角落。

答案 1 :(得分:0)

使用它来提供转角半径

let Path = UIBezierPath(roundedRect: button.bounds, byRoundingCorners: [UIRectCorner.TopLeft , UIRectCorner.BottomLeft], cornerRadii: CGSizeMake(1.0, 1.0)).CGPath

答案 2 :(得分:0)

此代码适用于swift 2.0,只需将此代码转换为swift 3.0

即可
cancelbnutton.layer.cornerRadius = 10.0
 cancelbnutton.clipsToBounds = true
 let rectShape = CAShapeLayer()
 rectShape.bounds = scanbutton.frame
 rectShape.position = scanbutton.center

 rectShape.path = UIBezierPath(roundedRect: scanbutton.bounds, byRoundingCorners: [UIRectCorner.TopLeft , UIRectCorner.TopRight], cornerRadii: CGSizeMake(10, 10)).CGPath
 scanbutton.layer.mask = rectShape

 let rectShape1 = CAShapeLayer()
 rectShape1.bounds = manualbutton.frame
 rectShape1.position = manualbutton.center

 rectShape1.path = UIBezierPath(roundedRect: manualbutton.bounds, byRoundingCorners: [UIRectCorner.BottomLeft , UIRectCorner.BottomRight], cornerRadii: CGSizeMake(10, 10)).CGPath
 manualbutton.layer.mask = rectShape1

output