【问题标题】:UITableViewCell TopLeft and BottomLeft Corners not workingUITableViewCell TopLeft 和 BottomLeft 角不起作用
【发布时间】:2017-06-26 09:56:16
【问题描述】:

我是 iOS 开发的新手,我正在尝试创建一个自定义 UITableViewCell,它的背景是我想将左上角和左下角圆角,但它只适用于左上角。

这是我的代码(Swift 3):

override func awakeFromNib() {
    super.awakeFromNib()

    let bgViewMask = CAShapeLayer()

    let bgViewPath = UIBezierPath(
        roundedRect: bgView.bounds,
        byRoundingCorners: [.topLeft, .bottomLeft],
        cornerRadii: CGSize(width: 8, height: 8)
    )

    bgViewMask.frame = bgView.bounds
    bgViewMask.path = bgViewPath.cgPath

    bgView.layer.mask = bgViewMask
}

我也在 layoutSubviews() 中试过这个

override func layoutSubviews() {
    super.layoutSubviews()

    //code here
}

但效果不佳。

如果我使用bgView.layer.cornerRadius = 8,它适用于所有角落。

我做错了吗?

【问题讨论】:

    标签: ios swift xcode swift3 xcode8


    【解决方案1】:

    你可以试试这个:

    import UIKit
    class CustomImageView: UIImageView {
    
    override public func layoutSubviews() {
        super.layoutSubviews()
        self.roundUpCorners([.topLeft, .bottomLeft], radius: 8)
    }
    }
    extension UIView {
    func roundUpCorners(_ corners: UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-27
      • 1970-01-01
      • 2020-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多