How to set cornerRadius for only top-left and top-right corner of a UIView?

后端 未结 26 3386
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 06:14

Is there a way to set cornerRadius for only top-left and top-right corner of a UIView?

I tried the following, but it end up not seeing the

26条回答
  •  眼角桃花
    2020-11-22 06:58

    A lovely extension to reuse Yunus Nedim Mehel solution

    Swift 2.3

    extension UIView {
    func roundCornersWithLayerMask(cornerRadii: CGFloat, corners: UIRectCorner) {
        let path = UIBezierPath(roundedRect: bounds,
                                byRoundingCorners: corners,
                                cornerRadii: CGSize(width: cornerRadii, height: cornerRadii))
        let maskLayer = CAShapeLayer()
        maskLayer.path = path.CGPath
        layer.mask = maskLayer
    } }
    

    Usage

    let view = UIView()
    view.roundCornersWithLayerMask(10,[.TopLeft,.TopRight])
    

提交回复
热议问题