Custom Rounding corners on UIView

The problem is that vwView gets resized later to fit the screen, but you don’t update the path for its new size. There’s no particularly trivial fix for this. You basically need to make vwView a custom class (if it’s not already) and update the mask in layoutSubviews. Example:

public class RoundedBorderView: UIView {

    public var roundedCorners: UIRectCorner = [ .BottomLeft, .BottomRight ] {
        didSet { self.setNeedsLayout() }
    }

    public var cornerRadii: CGSize = CGSizeMake(10, 10) {
        didSet { self.setNeedsLayout() }
    }

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

    private func updateMask() {
        let mask = maskShapeLayer()
        mask.path = UIBezierPath(roundedRect: bounds, byRoundingCorners: roundedCorners, cornerRadii: cornerRadii).CGPath
    }

    private func maskShapeLayer() -> CAShapeLayer {
        if let mask = layer.mask as? CAShapeLayer {
            return mask
        }
        let mask = CAShapeLayer()
        layer.mask = mask
        return mask
    }

}

Change the class of vwView to RoundedBorderView and it will maintain its own mask layer.

Leave a Comment