简体   繁体   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

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!请帮忙!

Why don't you simply use maskedCorners and cornerRadius for that?你为什么不简单地使用maskedCornerscornerRadius呢?

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

Screenshot:截屏:

在此处输入图像描述

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.

相关问题 如何检查我是否在iPhone6s Plus模拟器上运行 - How to check if I am running on iPhone6s Plus simulator 在没有iPhone6s或更新版本的情况下测试ARKit - Testing ARKit without iPhone6s or newer Appium:轻按iPhone4s和iPhone6s - Appium: Tap in iPhone4s and iPhone6s 使用自动布局通过iPhone6s plus为iPhone4s开发UI - Use auto layout to develop UI for iphone4s through iPhone6s plus UIModalTransitionStyleCrossDissolve的转换在IOS Sdk 9.3中显示iphone6的错误 - Transition with UIModalTransitionStyleCrossDissolve shows error with iphone6s in IOS Sdk 9.3 无法在TopViewController XCode 8.1,iphone6s / 6s + / 7/7 +上点击呈现的ViewController - Can not tap on presented ViewController on TopViewController XCode 8.1, iphone6s/6s+/7/7+ 在 iPhone 6S 或 iPhone 6S Plus 模拟器上模拟力触摸/3D 触摸 - Simulate force touch / 3D touch on iPhone 6S or iPhone 6S Plus simulators iPhone的圆角视图 - rounded corner on views of iPhone iOS框架适用于iPhone 5s,6和6 Plus,但不适用于其他iPhone或iPad模拟器 - iOS framework works for iPhone 5s, 6, and 6 Plus but does not compile for other iPhone or iPad simulators 自动版式无法在iPhone 6s Plus中正常运行 - Autolayouts not working as expected in iPhone 6s Plus
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM