gpt4 book ai didi

ios - 按钮的圆角半径与下面的 View 对齐

转载 作者:可可西里 更新时间:2023-11-01 01:31:18 24 4
gpt4 key购买 nike

SO 上有很多关于圆角的答案。我遇到的问题是我试图将按钮角与下方 View 的圆角对齐。请看图片。黄色 View 有 4 个角。我试图让关闭按钮在右上角四舍五入以与黄色 View 的圆角对齐。我使用了下面的 Swift 3 代码,但按钮保持方形。任何人都可以指出缺少的内容吗?

viewRaised 是黄色 View 。

非常感谢!

let path = UIBezierPath(roundedRect: btnCancel.layer.bounds, byRoundingCorners:[.topRight, .bottomLeft], cornerRadii: CGSize(width: 10, height:  10))
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
btnCancel.layer.mask = maskLayer
btnCancel.layer.masksToBounds = true

self.viewRaised.layer.cornerRadius = 10
self.viewRaised.layer.masksToBounds = false
self.viewRaised.layer.shadowOffset = CGSize(width: 5, height: 10);
self.viewRaised.layer.shadowRadius = 10;
self.viewRaised.layer.shadowOpacity = 0.5;

enter image description here

更新:

有趣的是,相同的代码似乎有效,但只适用于左上角。请参见第二张图片。

    self.viewRaised.layer.cornerRadius = 10
self.viewRaised.layer.masksToBounds = false
self.viewRaised.layer.shadowOffset = CGSize(width: 5, height: 10);
self.viewRaised.layer.shadowRadius = 10;
self.viewRaised.layer.shadowOpacity = 0.5;

let path = UIBezierPath(roundedRect: btnCancel.layer.bounds, byRoundingCorners:[.allCorners], cornerRadii: CGSize(width: 10, height: 10))
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
btnCancel.layer.mask = maskLayer

这很令人费解。我正在使用 XCode 版本 8.0 beta 6 (8S201h)

enter image description here

最佳答案

  1. 您的 btnCancel.layer.mask 应设置为黄色 View
  2. 您需要将btnCancel 添加为黄色 View 的父级 的子层。

示例(Swift 3.x):

let yellowView = UIView(frame: CGRectMake(20.0, 20.0, 200.0, 200.0))
yellowView.backgroundColor = UIColor.yellowColor()
yellowView.layer.borderColor = UIColor.clearColor().CGColor
yellowView.layer.borderWidth = 1.0
yellowView.layer.cornerRadius = 10.0
self.view.addSubview(yellowView) // Add yellowView to self's main view

let btnCancel = UIView(frame: CGRectMake(20.0, 20.0, 45.0, 45.0))
btnCancel.backgroundColor = UIColor.redColor()
btnCancel.layer.mask = yellowView.layer // Set btnCancel.layer.mask to yellowView.layer
self.view.addSubview(btnCancel) // Add btnCancel to self's main view, NOT yellowView

注意:

You don't need to enable clipsToBounds because you're setting a mask layer.

You Also don't need to create a new CAShapeLayer for the mask. Use yellowView's layer as the mask.

swift 4.x:

let yellowView = UIView(frame: CGRect(x: 20.0, y: 20.0, width: 200.0, height: 200.0))
yellowView.backgroundColor = .yellow
yellowView.layer.borderColor = UIColor.clear.cgColor
yellowView.layer.borderWidth = 1.0
yellowView.layer.cornerRadius = 10.0
self.view.addSubview(yellowView) // Add yellowView to self's main view

let btnCancel = UIView(frame: CGRect(x: 20.0, y: 20.0, width: 45.0, height: 45.0))
btnCancel.backgroundColor = .red
btnCancel.layer.mask = yellowView.layer // Set btnCancel.layer.mask to yellowView.layer
self.view.addSubview(btnCancel) // Add btnCancel to self's main view, NOT yellowView

关于ios - 按钮的圆角半径与下面的 View 对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39235662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com