如何在iPhone上设置按钮背景颜色? [英] How can I set a button background color on iPhone?
问题描述
如何设置按钮的自定义背景颜色?
How can I set a custom background color of a button?
Interface Builder似乎没有执行此操作的界面。
Interface Builder doesn't seem to have an interface to do this.
它只能通过编程方式使用吗?
Is it only available programmatically? If so, can you provide an example, please?
推荐答案
我读你的问题 require (如我所做)以程序化方式设置按钮颜色。这是从UIKit的一个明显的遗漏,我不能得到未记录的UIGlassButton工作。
I read your question to require (as I do) a programmatic way to set button color. It's a glaring omission from the UIKit, and I couldn't get the undocumented UIGlassButton to work.
我已经解决了 tintColor
: UISegmentedControl
可以设置:
I've solved this with a single segment UISegmentedControl
, which allows you to set the tintColor
:
UISegmentedControl * btn = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:name]];
btn.momentary = YES;
btn.segmentedControlStyle = UISegmentedControlStyleBar;
btn.tintColor = color;
[btn addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];
请注意,瞬间
和 segmentedControlStyle
属性无关紧要,并且可以使用图像代替 NSString *
名称。
Note please that the momentary
and segmentedControlStyle
properties do matter, and that an image can be used instead of a NSString *
name.
如果你可以使用有限的罐头图像,但是这不符合要求!例如
A stretchable image with end caps works fine if you can use a finite set of canned images, but that doesn't fit the requirement! E.g.,
buttonImage = [[UIImage imageNamed:@"button.png"] stretchableImageWithLeftCapWidth:26 topCapHeight:16];
这篇关于如何在iPhone上设置按钮背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!