我将角半径仅应用于视图的左上角和右上角。在viewDidLoad()
我有:
if #available(iOS 11.0, *) {
view.layer.cornerRadius = 20.0
view.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
}
如果iOS 11不可用,最佳方式似乎是draw(_ rect:)
。由于我必须在viewDidLoad()
之外覆盖它,我想执行以下操作
if NOT #available(iOS 11.0, *) {
override func draw(_ rect: CGRect) {
let maskPath = UIBezierPath(roundedRect: self.contentView.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 20.0, height: 20.0))
let shapeLayer = CAShapeLayer()
shapeLayer.frame = self.bounds
shapeLayer.path = maskPath.cgPath
view.layer.mask = shapeLayer
}
}
当然在语法上是不正确的。我该怎么办?
答案 0 :(得分:5)
如果你需要支持早于iOS 11的版本,那么在函数#available
中使用draw(rect:)
并使用else部分在早期版本上应用逻辑
override func draw(_ rect: CGRect) {
if #available(iOS 11, *) {} else {
let maskPath = UIBezierPath(roundedRect: self.contentView.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 20.0, height: 20.0))
let shapeLayer = CAShapeLayer()
shapeLayer.frame = self.bounds
shapeLayer.path = maskPath.cgPath
view.layer.mask = shapeLayer
}
}