繁体   English   中英

UIView 上的圆角仅在 iPhone8、iPhone6s、iPhone6s plus、iPhone 6 等少数模拟器中工作

[英]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 等模拟器的 rest,代码无法按要求工作。 我什至尝试使用多种类型的视图,但它没有按照要求工作。 我只在左侧得到圆角,但在右侧没有。 下面是来自不同模拟器的 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)

数字可以改变。 这意味着宽度高达手机屏幕的 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM