UIButtonTypeRoundedRect类型的UIButton的着色

时间:2010-01-18 18:28:38

标签: iphone objective-c cocoa-touch colors uibutton

我希望UIButton主题类型的圆角矩形部分是我指定的纯色自定义颜色。我明白了

。 setTitleColor:更改文本颜色 。 backgroundColor:更改圆角矩形后面的四个饼形角件的颜色

问题是如何更改圆角矩形部分的颜色。

我尝试过setImage,但图片必须有圆角,当按钮改变大小时,图片没有任何价值。它不会扩展到新的大小。

提前致谢。

4 个答案:

答案 0 :(得分:2)

尝试

CALayer *subLayer = [CALayer layer];
subLayer.frame = self.theButton.bounds;
subLayer.cornerRadius = 10.0;
subLayer.opacity = 1.0;

CALayer *imageLayer = [CALayer layer];
imageLayer.frame = subLayer.bounds;
imageLayer.cornerRadius = 10.0;


imageLayer.masksToBounds = YES;
imageLayer.opacity = 1.0;
imageLayer.contents = (id) [UIImage imageNamed:@"your.png"].CGImage;

[subLayer addSublayer:imageLayer];

[self.theButton.layer addSublayer:subLayer];

答案 1 :(得分:1)

您可以尝试抓取图层并在自定义按钮中设置其角半径。我没试过,但似乎值得一试。

[[button layer] setCornerRadius:12.0f];
[[button layer] setMasksToBounds:YES];
[[button layer] setBackgroundColor:[[UIColor redColor] CGColor]];

您必须将QuartzCore框架添加到项目(和#import)才能利用这些方法。

只是一个补充,在这种情况下,您使用按钮类型作为“自定义”。所以按钮可以声明为: UIButton * button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

答案 2 :(得分:0)

使用UIButton's titleLabelbackgroundColor是否有效?

答案 3 :(得分:0)

我认为最简单的方法就是创建一个自己的实心圆形按钮png图像,并在圆角处设置透明胶片。

然后,只需将自定义按钮的背景设置为您创建的图像。

除此之外,我实际上并不认为这可以做到......不确定......