我为此做了一个自定义类。但这仅适用于左上角,不适用于其他:

@IBDesignable
public class RoundedView: UIView {

    @IBInspectable public var topLeft: Bool = false
    @IBInspectable public var topRight: Bool = false
    @IBInspectable public var bottomLeft: Bool = false
    @IBInspectable public var bottomRight: Bool = false
    @IBInspectable public var cornerRadius: Int = 0

    public override func awakeFromNib() {
        var options = UIRectCorner()

        if topLeft { options =  options.union(.topLeft) }
        if topRight { options =  options.union(.topRight) }
        if bottomLeft { options =  options.union(.bottomLeft) }
        if bottomRight { options =  options.union(.bottomRight) }

        let path = UIBezierPath(roundedRect:self.bounds,
                                byRoundingCorners:options,
                                cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))

        let maskLayer = CAShapeLayer()

        maskLayer.path = path.cgPath
        self.layer.mask = maskLayer
    }
}

这是在模拟器中运行时的外观,我将所有4个角都应用了角半径:

ios - 不适用于Storyboard中特定UIView角的角半径不适用于所有角-LMLPHP

这里发生了什么 ? TopLeft起作用,TopRight稍微起作用,而下角完全不起作用。我很困惑。

最佳答案

您应该在layoutSubviews的替代中更新掩码。由于约束条件会更新视图的frame,因此会调用layoutSubviews,因此这是更新蒙版的正确位置。

顺便说一句,我不鼓励使用awakeFromNib来配置视图。如果您使用情节提要,效果很好,但是以编程方式创建的视图将不会调用此功能。如果您确实想在创建视图时对其进行配置,则最好将代码放入init(frame:)init(coder:)调用的某种 private 配置方法中。这样,它既适用于情节提要和以编程方式创建的视图。

我可能还会建议观察者检查可检查的属性,以触发对角圆角的更新。如果要更改这些属性,则希望圆角自动更新。

从而:

@IBDesignable
public class RoundedView: UIView {

    @IBInspectable public var topLeft: Bool = false      { didSet { setNeedsLayout() } }
    @IBInspectable public var topRight: Bool = false     { didSet { setNeedsLayout() } }
    @IBInspectable public var bottomLeft: Bool = false   { didSet { setNeedsLayout() } }
    @IBInspectable public var bottomRight: Bool = false  { didSet { setNeedsLayout() } }
    @IBInspectable public var cornerRadius: CGFloat = 0  { didSet { setNeedsLayout() } }

    public override func layoutSubviews() {
        super.layoutSubviews()

        var options = UIRectCorner()

        if topLeft     { options.formUnion(.topLeft) }
        if topRight    { options.formUnion(.topRight) }
        if bottomLeft  { options.formUnion(.bottomLeft) }
        if bottomRight { options.formUnion(.bottomRight) }

        let path = UIBezierPath(roundedRect: bounds,
                                byRoundingCorners: options,
                                cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))

        let maskLayer = CAShapeLayer()

        maskLayer.path = path.cgPath
        layer.mask = maskLayer
    }
}

或者,或者,如果定位到iOS 11及更高版本,我们可以让CoreAnimation进行四舍五入:
@IBDesignable
public class RoundedView: UIView {

    @IBInspectable public var topLeft: Bool = false      { didSet { updateCorners() } }
    @IBInspectable public var topRight: Bool = false     { didSet { updateCorners() } }
    @IBInspectable public var bottomLeft: Bool = false   { didSet { updateCorners() } }
    @IBInspectable public var bottomRight: Bool = false  { didSet { updateCorners() } }
    @IBInspectable public var cornerRadius: CGFloat = 0  { didSet { updateCorners() } }

    public override init(frame: CGRect = .zero) {
        super.init(frame: frame)
        updateCorners()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        updateCorners()
    }
}

private extension RoundedView {
    func updateCorners() {
        var corners = CACornerMask()

        if topLeft     { corners.formUnion(.layerMinXMinYCorner) }
        if topRight    { corners.formUnion(.layerMaxXMinYCorner) }
        if bottomLeft  { corners.formUnion(.layerMinXMaxYCorner) }
        if bottomRight { corners.formUnion(.layerMaxXMaxYCorner) }

        layer.maskedCorners = corners
        layer.cornerRadius = cornerRadius
    }
}

关于后一种方法的好处是,如果我们碰巧对视图的frame进行动画处理,则CoreAnimation将兑现我们的圆角处理:

ios - 不适用于Storyboard中特定UIView角的角半径不适用于所有角-LMLPHP

如果您使用上述的layoutSubviews方法,则必须手动管理动画(例如,使用CADisplayLink)。

10-07 19:51
查看更多