ios - 右上角在 swift 4 中不起作用 byRoundingcorners
问题描述
我正在尝试使用以下代码为左上角和右上角的子视图制作圆角
maskLayer.path = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: [.topRight, .topLeft], cornerRadii: CGSize(width: 10, height: 10)).cgPath
也在 viewDidLayoutSubviews() 中尝试过,但只有左边框半径有效……右边框半径无效。
有人可以帮我吗
解决方案
你可能会错过配置一些东西,这里是maskedCorners
左上角&&右roundCorners
圆——和左下角&&右圆
class ViewController: UIViewController {
let redBox = UIView(frame: CGRect(x: 100, y: 100, width: 128, height: 128))
let blueBox = UIView(frame: CGRect(x: 100, y: 300, width: 128, height: 128))
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
redBox.backgroundColor = .red
redBox.layer.cornerRadius = 25
redBox.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
view.addSubview(redBox)
blueBox.backgroundColor = .blue
view.addSubview(blueBox)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
blueBox.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 25.0)
}
}
extension UIView {
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
layer.mask = mask
}
}
推荐阅读
- c# - 为文件夹中的所有文件设置使用命名空间
- html - 单击链接 href 目标 id 纯 css 时在滑块顶部停止滚动
- amazon-web-services - API 网关 - 端点请求随机超时
- wordpress - 发布时是否有任何方法可以在 wordpress 帖子中生成自动标题
- wordpress - 使用 Timber (Twig) 构建的 WordPress 网站是否可以使用 AMP
- c# - 运行时 C# 列表框选定索引保持在 -1
- javascript - 用于连接到不同 WebSocket 服务器的多个 WebSocket 客户端的 Node.js 集群?
- c# - 如何在 C# 中提高读取 tesseract 的准确性
- apache-kafka - 在我的 Kafka 主题中,End-of-the-Offset 高于最后一条消息的偏移量,因此我可以看到消费者滞后 1
- javascript - 为什么在 .slice() 和连接的这个用例中混合引号时不需要加 1?