how to set cornerRadius for only bottom-left,bottom-right and top-left corner of a UIView?

前端 未结 10 1177
Happy的楠姐
Happy的楠姐 2020-12-04 06:49

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

I tried the following, but it ended up making the view disappea

10条回答
  •  旧巷少年郎
    2020-12-04 07:33

    On iOS 11, What you only need is the maskedCorners.

        let cornerRadius: CGFloat = 6.0
        if #available(iOS 11, *) {
            productImageView.layer.cornerRadius  = cornerRadius
            productImageView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
    
        } else {
            let path        = UIBezierPath(roundedRect: bounds, byRoundingCorners: [.topLeft , .topRight], cornerRadii:CGSize(width: cornerRadius, height: cornerRadius))
            let maskLayer   = CAShapeLayer()
            maskLayer.frame = bounds
            maskLayer.path  = path.cgPath
            productImageView.mask                = maskLayer
            productImageView.layer.masksToBounds = true
        }
    

提交回复
热议问题