How to set corner radius of imageView?

后端 未结 10 689
清酒与你
清酒与你 2020-12-13 01:50

In Objective-C such line

self.mainImageView.layer.cornerRadius = CGRectGetWidth(self.mainImageView.frame)/4.0f;

does its job, I tried it in

10条回答
  •  余生分开走
    2020-12-13 02:26

    I created an UIView extension which allows to round specific corners :

    import UIKit
    
    enum RoundType {
        case top
        case none
        case bottom
        case both
    }
    
    extension UIView {
    
        func round(with type: RoundType, radius: CGFloat = 3.0) {
            var corners: UIRectCorner
    
            switch type {
            case .top:
                corners = [.topLeft, .topRight]
            case .none:
                corners = []
            case .bottom:
                corners = [.bottomLeft, .bottomRight]
            case .both:
                corners = [.allCorners]
            }
    
            DispatchQueue.main.async {
                let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
                let mask = CAShapeLayer()
                mask.path = path.cgPath
                self.layer.mask = mask
            }
        }
    
    }
    

提交回复
热议问题