How to set corner radius of imageView?
Asked Answered
D

10

79

In Objective-C such line

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

does its job, I tried it in Swift using analogy

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

and it doesn't change anything, the corners are the same as before. Moreover, Xcode does not show any syntax errors. Does Swift support any other way to reach this goal? I checked some other threads here and usually it's getting done in Swift in the way showed above.

Downtrend answered 9/1, 2015 at 13:13 Comment(3)
self.mainImageView.layer.cornerRadius = (self.mainImageView.frame)/2.0Levitical
"CGRect is not convertible to double"Downtrend
self.mainImageView.layer.cornerRadius = (self.mainImageView.frame.size.height)/2.0 doesn't work as wellDowntrend
P
190

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

Plascencia answered 9/1, 2015 at 14:2 Comment(3)
I missed that line, now works perfectly fine, thank you @Plascencia !Downtrend
this answer is correct for roundcorner after get image from url and set to imageviewOuthe
my guuuyyyy nice!Aldin
D
38

There is one tiny difference in Swift 3.0 and Xcode8

Whenever you want to apply corner radius to UIView, make sure you call yourUIView.layoutIfNeeded() before calling cornerRadius.

Otherwise, it will return the default value for UIView's height and width (1000.0) which will probably make your View disappear.

Always make sure that all effects that changes the size of UIView (Interface builder constraints etc) are applied before setting any layer properties.

Example of UIView class implementation

class BadgeView: UIView {

  override func awakeFromNib() {

    self.layoutIfNeeded()
    layer.cornerRadius = self.frame.height / 2.0
    layer.masksToBounds = true

   }
 }
Downtrend answered 25/9, 2016 at 6:45 Comment(1)
masksToBounds was also needed for me to get it to work after the update to Swift 3.0Glutton
L
28

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

Result in emulator

Letendre answered 25/9, 2017 at 2:21 Comment(1)
This is a clean & simple solutionSagacity
P
19

try this

self.mainImageView.layer.cornerRadius = CGRectGetWidth(self.mainImageView.frame)/4.0
self.mainImageView.clipsToBounds = true
Phillida answered 18/11, 2015 at 17:45 Comment(0)
M
11

Marked with @IBInspectable in swift (or IBInspectable in Objective-C), they are easily editable in Interface Builder’s attributes inspector panel.
You can directly set borderWidth,cornerRadius,borderColor in attributes inspector

extension UIView {

  @IBInspectable var cornerRadius: CGFloat {

   get{
        return layer.cornerRadius
    }
    set {
        layer.cornerRadius = newValue
        layer.masksToBounds = newValue > 0
    }
  }

  @IBInspectable var borderWidth: CGFloat {
    get {
        return layer.borderWidth
    }
    set {
        layer.borderWidth = newValue
    }
  }

  @IBInspectable var borderColor: UIColor? {
    get {
        return UIColor(cgColor: layer.borderColor!)
    }
    set {
        layer.borderColor = borderColor?.cgColor
    }
  }
}

enter image description here

Melisenda answered 2/6, 2017 at 5:44 Comment(2)
Border Color is not working. It returns black always.Bor
@JoeSene layer.borderColor = borderColor?.cgColor is wrong, just update it like this: layer.borderColor = newValue?.cgColorContinuator
G
7

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

    view.layer.cornerRadius = view.frame.width/4.0
    view.clipsToBounds = true
Godbeare answered 4/8, 2016 at 8:16 Comment(0)
R
6

Swift 3, Xcode 8, iOS 10

DispatchQueue.main.async {
  self.mainImageView.layer.cornerRadius = self.mainImageView.bounds.size.width / 2.0
  self.mainImageView.clipsToBounds = true
}
Rok answered 26/9, 2016 at 9:39 Comment(1)
Calling from the main thread helped me because I rounded containerView - which was a subview of my tableView cells - in the willDisplay tableView delegate method. Without calling it from the main thread, the frame was wrongBirkner
B
5

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
        }
    }

}
Birkner answered 11/9, 2018 at 9:3 Comment(0)
C
4
import UIKit

class BorderImage: UIImageView {

    override func awakeFromNib() {
        self.layoutIfNeeded()
        layer.cornerRadius = self.frame.height / 10.0
        layer.masksToBounds = true
    }
}

Based on @DCDC's answer

Companionable answered 28/1, 2020 at 5:16 Comment(0)
K
3

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
Kyrakyriako answered 5/10, 2016 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.