ios - 圆角不适用于除 iPhone XS max 和 XR 以外的所有模拟器

标签 ios swift xcode uicollectionviewcell rounded-corners

我正在为我的 Collection View 单元格使用圆角,这在 iPhone XS Max 和 XR 中工作正常,但圆角在其他人中没有显示。下面是我正在使用的代码。


 class ButtonCollectionViewCell: UICollectionViewCell {

        @IBOutlet weak var buttonImage: UIImageView!
        @IBOutlet weak var buttonTitle: UILabel!

        @IBOutlet weak var cellBackGroundView: UIView!
        @IBOutlet weak var imageWidthContraint: NSLayoutConstraint!
        @IBOutlet weak var imageHeightContraint: NSLayoutConstraint!

        override func awakeFromNib() {
             super.awakeFromNib()
             self.applyShadow(shadowColor: UIColor.gray, shadowRadious: 4, shadowOpacity: 1)
           makeRoundedCorners()

        }

        func makeRoundedCorners() {
             cellBackGroundView.roundCorners(corners: .allCorners, radius: 15)
        }

        func setupImageSize(cellType : String){
            if cellType == "12" {
                imageWidthContraint.constant = 40
                imageHeightContraint.constant = 50
            }else if cellType == "13"{
                imageWidthContraint.constant = 62
                imageHeightContraint.constant = 34
            }
        }

    }

extension UIView {
    func roundCorners(corners: UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))

        let mask = CAShapeLayer()
        layer.mask = mask
        mask.path = path.cgPath

    }

    }

最佳答案

你可以试试这个

extension UIView {

    func roundCorners(radius: CGFloat) {

        self.layer.masksToBounds = true
        self.layer.cornerRadius = radius
    }
}

关于ios - 圆角不适用于除 iPhone XS max 和 XR 以外的所有模拟器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58979866/

相关文章:

iphone - 将图像添加到 MapView 注释

ios - removeFromSuperview UIImageView 不工作

ios - writeToFile 不适用于保存照片

ios - Xcodebuild - 跳过完成请求崩溃报告。继续测试

ios - 如何创建具有内部框架的框架?

ios - 错误的接收器类型 iOS

iphone - iPhone:如何在iPod声音之上播放声音?

ios - 使用Swift将一些Class类型的Arraylist转换成JSON格式

swift - 如何在swift中同步循环

ios - 连接到 XCode 后,iPhone 位置服务显示错误的位置