简体   繁体   English

UIButton的转角半径问题

[英]Corner radius issue with UIButton

I want to add a corner radius to a UIButton. 我想为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? 您是否尝试使用maskToBounds:属性设置? 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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