dub*_*eat 105 iphone cocoa-touch objective-c uibutton
这个让我难过.
是否有可能在iPhone的Cocoa中更改UIButton的背景颜色.我尝试过设置背景颜色,但只改变了角落.setBackgroundColor:似乎是唯一可用于此类事情的方法.
[random setBackgroundColor:[UIColor blueColor]];
[random.titleLabel setBackgroundColor:[UIColor blueColor]];
Run Code Online (Sandbox Code Playgroud)
Sti*_*vik 159
这可以通过制作副本以编程方式完成:
loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
[loginButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
loginButton.backgroundColor = [UIColor whiteColor];
loginButton.layer.borderColor = [UIColor blackColor].CGColor;
loginButton.layer.borderWidth = 0.5f;
loginButton.layer.cornerRadius = 10.0f;
Run Code Online (Sandbox Code Playgroud)
编辑:当然,你必须 #import <QuartzCore/QuartzCore.h>
编辑:对于所有新读者,您还应该考虑添加一些选项作为"另一种可能性".供您考虑.
由于这是一个陈旧的答案,我强烈建议阅读评论以进行故障排除
kar*_*rim 59
我有一个不同的方法,
[btFind setTitle:NSLocalizedString(@"Find", @"") forState:UIControlStateNormal];
[btFind setBackgroundImage:[CommonUIUtility imageFromColor:[UIColor cyanColor]]
forState:UIControlStateNormal];
btFind.layer.cornerRadius = 8.0;
btFind.layer.masksToBounds = YES;
btFind.layer.borderColor = [UIColor lightGrayColor].CGColor;
btFind.layer.borderWidth = 1;
Run Code Online (Sandbox Code Playgroud)
从CommonUIUtility,
+ (UIImage *) imageFromColor:(UIColor *)color {
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
// [[UIColor colorWithRed:222./255 green:227./255 blue: 229./255 alpha:1] CGColor]) ;
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
Run Code Online (Sandbox Code Playgroud)
别忘了 #import <QuartzCore/QuartzCore.h>
sti*_*igi 32
我假设你在谈论一个UIButton UIButtonTypeRoundedRect?你无法改变它的背景颜色.当您尝试更改它的背景颜色时,您更改的是绘制按钮的矩形颜色(通常很清楚).所以有两种方法可以去.您可以将UIButton子类化并覆盖其-drawRect:方法,也可以为不同的按钮状态创建图像(这样做非常好).
如果在Interface Builder中设置背景图像,您应该注意到IB不支持为按钮可以拥有的所有状态设置图像,因此我建议使用以下代码设置图像:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setBackgroundImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
[myButton setBackgroundImage:[UIImage imageNamed:@"disabled.png"] forState:UIControlStateDisabled];
[myButton setBackgroundImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateSelected];
[myButton setBackgroundImage:[UIImage imageNamed:@"higligted.png"] forState:UIControlStateHighlighted];
[myButton setBackgroundImage:[UIImage imageNamed:@"highlighted+selected.png"] forState:(UIControlStateHighlighted | UIControlStateSelected)];
Run Code Online (Sandbox Code Playgroud)
最后一行显示如何为所选和突出显示的状态设置图像(这是IB无法设置的状态).如果您的按钮不需要选择状态,则不需要所选图像(第4和第6行).
小智 28
另一种可能性
但是,按钮是方形的,这不是我们想要的.创建一个带有此按钮引用的IBOutlet,并将以下内容添加到viewDidLoad方法:
[buttonOutlet.layer setCornerRadius:7.0f];
[buttonOutlet.layer setClipToBounds:YES];
Run Code Online (Sandbox Code Playgroud)
不要忘记导入QuartzCore.h
Mug*_*nth 17
子类UIButton并覆盖setHighlighted和setSelected方法
-(void) setHighlighted:(BOOL)highlighted {
if(highlighted) {
self.backgroundColor = [self.mainColor darkerShade];
} else {
self.backgroundColor = self.mainColor;
}
[super setHighlighted:highlighted];
}
-(void) setSelected:(BOOL)selected {
if(selected) {
self.backgroundColor = [self.mainColor darkerShade];
} else {
self.backgroundColor = self.mainColor;
}
[super setSelected:selected];
}
Run Code Online (Sandbox Code Playgroud)
我的darkerShade方法是这样的UIColor类
-(UIColor*) darkerShade {
float red, green, blue, alpha;
[self getRed:&red green:&green blue:&blue alpha:&alpha];
double multiplier = 0.8f;
return [UIColor colorWithRed:red * multiplier green:green * multiplier blue:blue*multiplier alpha:alpha];
}
Run Code Online (Sandbox Code Playgroud)

如果您不想使用图像,并希望它看起来与圆角矩形样式完全相同,请尝试此操作.只需在UIButton上放置UIView,使用相同的框架和自动调整大小的蒙版,将alpha设置为0.3,并将背景设置为颜色.然后使用下面的代码片段从彩色叠加视图中剪切圆角边缘.此外,取消选中UIView上IB中的"User Interaction Enabled"复选框,以允许触摸事件级联到下面的UIButton.
一个副作用是你的文字也会着色.
#import <QuartzCore/QuartzCore.h>
colorizeOverlayView.layer.cornerRadius = 10.0f;
colorizeOverlayView.layer.masksToBounds = YES;
Run Code Online (Sandbox Code Playgroud)
小智 7
另一种可能性(最好和最美丽的imho):
在Interface Builder中使用所需背景颜色的2个段创建UISegmentedControl.将类型设置为"bar".然后,将其更改为只有一个段."接口"构建器不接受一个段,因此您必须以编程方式执行此操作.
因此,为此按钮创建一个IBOutlet并将其添加到视图的viewDidLoad:
[segmentedButton removeSegmentAtIndex:1 animated:NO];
Run Code Online (Sandbox Code Playgroud)
现在你有一个漂亮的光泽彩色按钮,具有指定的背景颜色.对于操作,请使用"value changed"事件.
(我在http://chris-software.com/index.php/2009/05/13/creating-a-nice-glass-buttons/上找到了这个.谢谢克里斯!
好吧,我百分之九十九,你不能只是去改变UIButton的背景颜色.相反,你必须自己去改变背景图像,我觉得这很痛苦.我很惊讶我必须这样做.
如果我错了或者如果有更好的方法而不必设置背景图片,请告诉我
[random setBackgroundImage:[UIImage imageNamed:@"toggleoff.png"] forState:UIControlStateNormal];
[random setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal];
[random setBackgroundImage:[UIImage imageNamed:@"toggleon.png"] forState:UIControlStateNormal];
[random setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)