UIlabel layer.cornerRadius not working in iOS 7.1
Asked Answered
U

8

198

I'm currently looking at a UILabel with the property addMessageLabel.layer.cornerRadius = 5.0f; On a device with iOS 7.0 installed, it has rounded corners. On a device with iOS 7.1 installed, it does not have rounded corners.

Is this just a bug with iOS 7.1?

Unlock answered 11/3, 2014 at 5:0 Comment(0)
C
519

Set the property clipsToBounds to true

addMessageLabel.clipsToBounds = true
Construct answered 11/3, 2014 at 5:18 Comment(8)
Not sure why you didn't have to do that on iOS 7 but have to iOS 7.1, but it worked! ThanksUnlock
This is indeed weird!Underwood
No, not weird...just "progress"...<humf>, it seems that UILabel's clipsToBounds is now defaulting to FALSE like most other UIViews. Apple's probably trying to make stuffs more consistent. I too just had the same issue.Decani
Is this documented in the iOS 7.1 release notes as a breaking change or is this just another 'surprise' that they threw in for us?Ruebenrueda
@ChristopherKing i couldn't find documentation on this, but this worked in my scenario too, surprise I guess :)Construct
= clip subviews with storyboards.Schadenfreude
@imankazemayni the post is old, it was answered in Objective C. Objective C has YES/NO for true/false, I am updating my answer as wellConstruct
it was better if your old answer exist. one for objective C and another for swift ... tnksPistil
S
67

I think the best way to set corner radius is:

enter image description here

and be sure the "Clip Subviews" is checked:

enter image description here

Checking "Clip Subviews" is equal to the code addMessageLabel.clipsToBounds = YES;.

Structure answered 3/4, 2015 at 18:59 Comment(4)
Definitely, the easiest wayFloatfeed
This is the best way. Tested with ios 8+ and xcode 7.2.Gauss
If anyone came here looking for this and it didnt work: if you put the property "cornerRadius" it will work on iOS10+. On iOS9 it has to be "layer.cornerRadius"Literature
clipToBounds can also be set in the User Defined Runtime Attributes (the same as the layer.cornerRadius): it should be key Path: clipsToBounds, type: Boolean, value: trueDuress
A
31

Try the followings,

[[addMessageLabel layer] setCornerRadius:5.0f];
[[addMessageLabel layer] setMasksToBounds:YES];

//or
[addMessageLabel setClipsToBounds:YES];

Swift

addMessageLable.layer.cornerRadius = 5.0
addMessageLable.layer.masksToBounds = true

//or
addMessageLable.layer.clipsToBounds = true
Adrienneadrift answered 11/3, 2014 at 5:8 Comment(0)
W
5

My issue was a bit different.

While I did do btn.clipsToBounds = true

I wasn't setting doing:

btn.layer.cornerRadius = 20

Because I had different screen sizes. Instead I followed this answer and did:

override func layoutSubviews() {
    seeMoreButton.layer.cornerRadius = seeMoreButton.bounds.size.height / 2
}

It wasn't working because I forgot to add super.layoutSubviews(). The correct code is:

override func layoutSubviews() {
    super.layoutSubviews()
    seeMoreButton.layer.cornerRadius = seeMoreButton.bounds.size.height / 2
}
Wizardry answered 25/10, 2017 at 18:23 Comment(1)
Thanks a ton.. Only this answer worked for me. Swift 3, Xcode 8.3.3Ento
V
3

I have tried the below one and i got an successful output.

yourlabelname.layer.cornerRadius = 10.0f;
[yourlabelname setClipsToBounds:YES];

Is there something else which is stopping you?

Vitiate answered 13/5, 2015 at 14:4 Comment(1)
Before iOS 7.1, clipsToBounds was defaulted to YES, so the line [yourlabelname setClipsToBounds:YES]; wasn't in my original code.Unlock
V
0
 //works perfect in Swift 2.0 for a circular or round image          


@IBOutlet var theImage: UIImageView!
        override func viewDidLoad() {
            super.viewDidLoad()
    //Make sure the width and height are same
            self.theImage.layer.cornerRadius = self.theImage.frame.size.width / 2
            self.theImage.layer.borderWidth = 2.0
            self.theImage.layer.borderColor = UIColor.whiteColor().CGColor
            self.theImage.clipsToBounds = true

        }
Valerivaleria answered 16/4, 2016 at 22:2 Comment(0)
S
0
yourlabelname.layer.cornerRadius = yourlabelname.frame.size.width/2;
[yourlabelname setClipsToBounds:YES];

Make sure you are checking with appropriate Deployment target.

Strother answered 15/6, 2017 at 17:49 Comment(0)
C
0

Add the Following Code as extension for UIView

//// Story board Extra Feature for create border radius, border width and border Color
extension UIView {
    /// corner radius
    @IBInspectable var borderColor: UIColor? {
        set {
            layer.borderColor = newValue!.cgColor
        }
        get {
            if let color = layer.borderColor {
                return UIColor(cgColor: color)
            } else {
                return nil
            }
        }
    }
    @IBInspectable var borderWidth: CGFloat {
        set {
            layer.borderWidth = newValue
        }
        get {
            return layer.borderWidth
        }
    }
    @IBInspectable var cornerRadius: CGFloat {
        set {
            layer.cornerRadius = newValue
            clipsToBounds = newValue > 0
        }
        get {
            return layer.cornerRadius
        }
    }
}

After that you will get the following attributes in interface builder itself.!

enter image description here

Citrine answered 11/7, 2019 at 7:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.