我想为我的 UI 元素设置圆角半径,如附件中所示:
我尝试使用 UIBezierPath
设置圆角半径,但它没有给出预期的结果。我还在 UIView
中添加了这三个 UIButton
并将圆角半径设置为容器 View ,但没有用。
引用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
以下是我在容器 View 上尝试过的代码:
containerView.layer.cornerRadius = 20
以下是我得到的输出:
感谢任何帮助。谢谢
最佳答案
在 byRoundingCorners: [.topLeft] 选项中为其他角使用 topRight、bottomLeft 和 bottomRight 以获得预期结果。
关于ios - 仅将圆角半径设置为 UIView/UIButton 的特定一侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40169745/