How to set corner radius of imageView?

后端 未结 10 630
清酒与你
清酒与你 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:24

    You can define border radius of any view providing an "User defined Runtime Attributes", providing key path "layer.cornerRadius" of type string and then the value of radius you need ;) See attached images below:

    Configuring in XCode

    0 讨论(0)
  • 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
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-12-13 02:30

    Layer draws out of clip region, you need to set it to mask to bounds:

    self.mainImageView.layer.masksToBounds = true
    

    From the docs:

    By default, the corner radius does not apply to the image in the layer’s contents property; it applies only to the background color and border of the layer. However, setting the masksToBounds property to true causes the content to be clipped to the rounded corners

    0 讨论(0)
  • 2020-12-13 02:30

    Swift 3, Xcode 8, iOS 10

    DispatchQueue.main.async {
      self.mainImageView.layer.cornerRadius = self.mainImageView.bounds.size.width / 2.0
      self.mainImageView.clipsToBounds = true
    }
    
    0 讨论(0)
  • 2020-12-13 02:30

    The easiest way is to create an UIImageView subclass (I have tried it and it's working perfectly on iPhone 7 and XCode 8):

    class CIRoundedImageView: UIImageView {
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }
    
        override func awakeFromNib() {
    
            self.layoutIfNeeded()
            layer.cornerRadius = self.frame.height / 2.0
            layer.masksToBounds = true
        }
    }
    

    and then you can also set a border:

    imageView.layer.borderWidth = 2.0
    
    imageView.layer.borderColor = UIColor.blackColor().CGColor
    
    0 讨论(0)
  • 2020-12-13 02:33

    in swift 3 'CGRectGetWidth' has been replaced by property 'CGRect.width'

        view.layer.cornerRadius = view.frame.width/4.0
        view.clipsToBounds = true
    
    0 讨论(0)
提交回复
热议问题