UIView 上的圆角仅在 iPhone8、iPhone6s、iPhone6s plus、iPhone6 等少数模拟器中不起作用

Rounded corner on UIView not working only in few simulators like iPhone8, iPhone6s, iPhone6s plus, iPhone 6

我需要在视图的左上角和右上角设置圆角。下面是相同的代码。

let size = CGSize(width: 30, height: 30)
        let bezierPath = UIBezierPath(roundedRect: self.alertView.bounds, byRoundingCorners: [.topRight, .topLeft], cornerRadii: size)
        let shapeLayer = CAShapeLayer()
        shapeLayer.frame = self.alertView.bounds
        shapeLayer.path = bezierPath.cgPath
        self.alertView.layer.mask = shapeLayer

这在所有模拟器 iPhone 8 plus 及更高版本中都运行良好。但对于其他模拟器,如 iPhone 6、iPhone 6 plus 等,代码无法按要求工作。我什至尝试使用多种类型的视图,但它没有按照要求工作。我只在左侧得到圆角,但在右侧没有。下面是不同模拟器的UIView截图

iPhone 11(工作正常)

iPhone 8(未按要求工作)

我没有得到这里的问题。请帮忙!

为什么不直接使用 maskedCornerscornerRadius 呢?

self.alertView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
self.alertView.layer.cornerRadius = 10.0

截图:

viewDidLayoutSubviews() 中添加圆角半径而不是 viewDidLoad()

 override func viewDidLayoutSubviews() {
     self.alertView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
     self.alertView.layer.cornerRadius = 10.0
}

尝试

let size = CGSize(width: view.frame.size.width * 0.80, height: 30)

let size = CGSize(width: UIScreen.main.bounds.width * 0.065, height: 30)

数字可以改变。这意味着宽度最多为 phone 屏幕的 80%。

我已经在每台设备上测试过了。请试试这个:

  let viewSub = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 200, height: 200))

viewSub.backgroundColor = .red
viewSub.center =  self.view.center
self.view.addSubview(viewSub)

let rectShape = CAShapeLayer()
rectShape.bounds = viewSub.frame
rectShape.position = viewSub.center
rectShape.path = UIBezierPath(roundedRect: viewSub.bounds, byRoundingCorners: [ .topRight , .topLeft], cornerRadii: CGSize(width: 20, height: 20)).cgPath

viewSub.layer.backgroundColor = UIColor.red.cgColor
//Here I'm masking the textView's layer with rectShape layer
viewSub.layer.mask = rectShape