[英]Rounded corner on UIView not working only in few simulators like iPhone8, iPhone6s, iPhone6s plus, iPhone 6
I have a requirement to get rounded corner on the top left and right of a view.我需要在视图的左上角和右上角获得圆角。 Below is the code for the same.
下面是相同的代码。
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
This is working fine in all the simulators iPhone 8 plus and above.这在所有模拟器 iPhone 8 plus 及更高版本中运行良好。 But for the rest of the simulators like iPhone 6, iPhone 6 plus etc the code is not working as required.
但是对于 iPhone 6、iPhone 6 plus 等模拟器的 rest,代码无法按要求工作。 I even tried using multiple types of views but it is not working as per the requirement.
我什至尝试使用多种类型的视图,但它没有按照要求工作。 I get only rounded corner on the left side but not on the right.
我只在左侧得到圆角,但在右侧没有。 Below are the screenshots of the UIView from different simulators
下面是来自不同模拟器的 UIView 的截图
iPhone 11 (working fine) iPhone 11(工作正常)
iPhone 8 (not working as per requirement) iPhone 8(不按要求工作)
I am not getting the issue here.我在这里没有得到这个问题。 Kindly help!
请帮忙!
Add corner Radius in viewDidLayoutSubviews()
instead of viewDidLoad()
在
viewDidLayoutSubviews()
中添加角半径而不是viewDidLoad()
override func viewDidLayoutSubviews() {
self.alertView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
self.alertView.layer.cornerRadius = 10.0
}
Try尝试
let size = CGSize(width: view.frame.size.width * 0.80, height: 30)
or或者
let size = CGSize(width: UIScreen.main.bounds.width * 0.065, height: 30)
Numbers can change.数字可以改变。 It means that the width is up to 80 percent of the phone's screen.
这意味着宽度高达手机屏幕的 80%。
I have tested this on every device.我在每台设备上都对此进行了测试。 please try this one:
请试试这个:
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.