How do I make an UIImage/-View with rounded corners CGRect (Swift)
Asked Answered
M

10

69

How do I make a UIImageView with rounded corners on a Swift iOS Playground?
Inside it needs to be filled with a color.

Margarettamargarette answered 24/8, 2014 at 20:43 Comment(1)
You can also Rounded particular edge : iosdevcenters.blogspot.com/2018/02/…Imphal
G
191
let imageView = UIImageView(frame: CGRectMake(0, 0, 100, 100))
imageView.backgroundColor = UIColor.redColor()
imageView.layer.cornerRadius = 8.0
imageView.clipsToBounds = true

Result:

enter image description here

Gallion answered 24/8, 2014 at 20:46 Comment(3)
what if we want to round the UIImage not UIImageView ????Berate
If you round the ImageView, the image will appear round. Same result.Gallion
@Arslan, see stackoverflow.com/questions/70252577, if you want to round the UIImage, rather than the UIImageView.Humidify
A
30

For rounded circle image frame in swift, what it worked for me was:

self.profileImageView.image =  UIImage(named:"profileUser")
self.profileImageView.layer.cornerRadius = self.profileImageView.frame.size.width / 2
self.profileImageView.clipsToBounds = true

And for adding a shadow:

self.profileImageView.layer.masksToBounds = NO;
self.profileImageView.layer.cornerRadius = 8;
self.profileImageView.shadowOffset = CGSizeMake(5.0, 5.0);
self.profileImageView.shadowRadius = 5;
self.profileImageView.shadowOpacity = 0.5;
Acrilan answered 20/3, 2015 at 8:45 Comment(0)
J
23

Try this, it worked for me.

self.profileImageView.layer.cornerRadius = self.profileImageView.frame.size.width / 2
self.profileImageView.clipsToBounds = true
Janaye answered 16/1, 2015 at 21:13 Comment(0)
C
14

I was tired of writing set radius and mask to bound for each UIView. So I made the following extenstion for UIView. Should work for every UIView subclass, though I have not tested it. The extension can be narrowed down for specific Views you use of course.

extension UIView {      
    func setRadius(radius: CGFloat? = nil) {
        self.layer.cornerRadius = radius ?? self.frame.width / 2;
        self.layer.masksToBounds = true;
    }
}

It will default to the view's half width if you don't pass it any specific value.

Calculation answered 25/4, 2016 at 19:25 Comment(0)
M
10

Swift 3.0, 4.0

If you want to use the storyboard. I applied this and make sure that "Clip to bounds" is enable.

enter image description here

Mathian answered 23/8, 2018 at 17:45 Comment(0)
N
4

If you want to have an option to round each UIImageView, you can copy this code into your project without forgetting to check clip to bounds and set its value to true

import UIKit

@IBDesignable
extension UIImageView
{
    private struct AssociatedKey
    {
        static var rounded = "UIImageView.rounded"
    }

    @IBInspectable var rounded: Bool
    {
        get
        {
            if let rounded = objc_getAssociatedObject(self, &AssociatedKey.rounded) as? Bool
            {
                return rounded
            }
            else
            {
                return false
            }
        }
        set
        {
            objc_setAssociatedObject(self, &AssociatedKey.rounded, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            layer.cornerRadius = CGFloat(newValue ? 1.0 : 0.0)*min(bounds.width, bounds.height)/2
        }
    }
}

screenshot

Nonsuch answered 16/7, 2018 at 10:10 Comment(2)
Work perfectly, ThanksCrawly
you can also add self.clipsToBounds = true in the setter to not have to worry about setting it in the interface builderCoryden
P
2

Swift 5.0:

My personal preference is to have an extra swift file for specific changes like this one. What I do is then create a class e.g. "RoundCorner" which is a subclass of the element I want to change in this case a View element. And then I am overriding the individual settings.

class RoundCorner: UIView {
override func draw(_ rect: CGRect) {
    self.layer.cornerRadius = 10 // change this number to get the corners you want
    self.layer.masksToBounds = true
    }
}

After that, you only have to select the element you want this changes on, and set the custom class to the class we created earlier.

Look at the screenshot here

Penitence answered 17/8, 2019 at 9:51 Comment(0)
V
1

Setting layer.cornerRadius = 10 at User Defined Runtime Attributes section on the Identity inspector works even on repeatable elements like table cells.

Versatile answered 11/10, 2018 at 5:11 Comment(0)
C
1

In Swift 4 you can do it fairly easily like this:

yourUIImage.layer.cornerRadius = 10 // Set it how you prefer
yourUIImage.layer.masksToBounds = true

And if you use a rather long rectangle image, for example for a featured content part in your app or whatever, make sure the Content Mode is set to Scale To Fit because otherwise the corners will be somehow rounded but will cut badly and it will not be a perfect round corner but rather a rounded, then sharp cut where it clips.

Chacha answered 31/10, 2021 at 17:20 Comment(1)
UIImage, dont even have layer...Dobbs
Z
0

in your viewDidload add this line of code

self.headerImageView.layer.cornerRadius = self.headerImageView.frame.size.width / 2
Zola answered 3/6, 2022 at 13:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.