ios - 仅将圆角半径设置为 UIView/UIButton 的特定一侧

标签 ios uiview uibutton swift3 cornerradius

我想为我的 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

感谢任何帮助。谢谢

最佳答案

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

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

相关文章:

ios - 来自 Xib 的自定义 UIView - 要求,良好实践

iphone - 自定义UITableView中的删除按钮

iOS 应用程序在使用 XCode 调试器启动时崩溃,在没有调试器的情况下运行良好

ios - 为什么 NSNumberFormatter 不接受加号

ios - MPMoviePlayerController 在自定义 UIView 中不可见

iphone - 显式调用 loadView——好/坏?

objective-c - 如何找到数组的索引?

ios - CGAffineTransform 旋转 UIButton

ios - 几秒钟后显示事件指示器?

ios - 在 Swift 中带有 header 的 URLRequest