Corner radius issue with UIButton
Asked Answered
P

3

9

I want to add a corner radius to a UIButton. It is working fine but a problem occurs when I add image to it.

It does not round its corners with images, the image is shown in full rectangle form.

Please see the image, I have used the corner radius with red color and the output is as follow:

enter image description here

Please help.

Placenta answered 14/4, 2011 at 8:6 Comment(0)
M
41

Did you try to use set the masksToBounds: property? Fore example:

CALayer *layer = [myView layer];
[layer setMasksToBounds:YES];
[layer setCornerRadius:8.0];

That should do the trick.

Manslaughter answered 14/4, 2011 at 8:10 Comment(2)
This will work if you don't have any shadow on the button. Otherwise, just do [[button.imageView layer] setCornerRadius:8.0];Keneth
Actually Abhijeet Barge's answer is better because with maskToBounds set to YES, you cannot have shadows (ie. layer.shadowXXX )Crossstaff
E
9

you use -

myButton.imageView.layer.cornerRadius = 5;

but make sure that your image size is exact same as button size. its working for me.

Eden answered 14/4, 2011 at 8:29 Comment(0)
T
1
yourButton.layer.cornerRadius = 10 //this value should be half of your button's height to make a circle
yourButton.clipsToBounds = true //this clips everything outside of bounds
Tynes answered 24/9, 2018 at 20:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.