UITableViewCell TopLeft 和 BottomLeft 角不起作用

UITableViewCell TopLeft and BottomLeft Corners not working

我是 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 它适用于所有角落。

我做错了什么吗?

你可以试试这个:

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
}
}