带有自定义边框颜色的UIButton,iPhone

时间:2011-09-09 05:28:52

标签: iphone uibutton

我想创建具有矩形形状的Custom UIButton。为此,我使用矩形视图作为UIButton的背景,并使UI按钮背景颜色为clearcolor。但UI按钮的边界仍然存在。关于如何使边界消失的任何建议? 感谢提前帮助..

5 个答案:

答案 0 :(得分:38)

试试这个:

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button.layer setBorderColor:[[UIColor clearColor] CGColor]];

但请勿忘记添加.h或.m文件

#import <QuartzCore/QuartzCore.h>

答案 1 :(得分:11)

我浪费了几分钟并且感到沮丧的是,即使我按照上面的建议设置了图层属性,边框也没有显示出来。

当我使用以下设置宽度时,我看到了按钮边框。

[[button layer] setBorderWidth:2.0f];

答案 2 :(得分:1)

您可以通过访问按钮的图层属性来设置CALayer上的边框属性。

添加石英

#import <QuartzCore/QuartzCore.h>

设置按钮的属性:

[[myButton layer] setBorderWidth:2.0f];
[[myButton layer] setBorderColor:[UIColor AnyColor].CGColor];

答案 3 :(得分:1)

header

橙色边框的快速示例

答案 4 :(得分:0)

您应该将UIButton的类型设置为Custom,而不是Rounded Rect(如果您通过 IB 创建按钮)。

如果您是以编程方式创建UIButton,那么请使用以下代码:

UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];

之后,您可以进行自定义。