OGeek|极客世界-中国程序员成长平台

标题: ios - 仅将角半径设置为 UIView/UIButton 的特定侧 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:54
标题: ios - 仅将角半径设置为 UIView/UIButton 的特定侧

我想为我的 UI 元素设置圆角半径,如附加快照所示:

enter image description here

我尝试使用 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

以下是我得到的输出:

enter image description here

任何帮助表示赞赏。谢谢



Best Answer-推荐答案


byRoundingCorners: [.topLeft] 选项中的 topRight、bottomLeft 和 bottomRight 用于其他角以获得预期结果。

关于ios - 仅将角半径设置为 UIView/UIButton 的特定侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40169745/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4