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

后端 未结 26 3452
佛祖请我去吃肉
佛祖请我去吃肉 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:48

    In Swift 4.2, Create it via @IBDesignable like this:

    @IBDesignable
    
    class DesignableViewCustomCorner: UIView {
    
        @IBInspectable var cornerRadious: CGFloat = 0 {
            didSet {
                let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: cornerRadious, height: cornerRadious))
                let mask = CAShapeLayer()
                mask.path = path.cgPath
                self.layer.mask = mask
            }
        }
    
    }
    

提交回复
热议问题