我试图使用以下代码为左上角和右上角的子视图制作圆角
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
}
}
关于ios - 右上角不能在byRoundingcorners中快速运行4,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53541852/