简体   繁体   中英

Corner radius issue with UIButton

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:

在此输入图像描述

Please help.

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.

you use -

myButton.imageView.layer.cornerRadius = 5;

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM