是否可以更改UIButtons背景颜色?

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>

编辑:对于所有新读者,您还应该考虑添加一些选项作为"另一种可能性".供您考虑.

由于这是一个陈旧的答案,我强烈建议阅读评论以进行故障排除

  • 甜!在所有的溶液中,我发现这是我带的那个.作为澄清的一点,制作"副本"意味着使用Quartz在自定义按钮中绘制圆角矩形,而不是使用RoundedRect类型按钮.起初我认为这意味着以某种方式复制具有不同颜色的圆角矩形以模拟改变bg颜色. (4认同)
  • 没有工作,或者你没有得到预期的结果? (3认同)

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>

  • 我们在iOS 7上这是非常愚蠢的,这仍然是最简单的添加背景颜色的方式 (3认同)
  • 好一个.我发现我必须添加一个`btFind.layer.borderWidth = 1;` (2认同)

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行).

  • 我刚刚对"iphone drawrect round corner"做了一个简短的谷歌查询,发现这个:http://iphonedevelopment.blogspot.com/2008/11/creating-transparent-uiviews-rounded.html检查drawrect方法的示例如何画一个圆形的矩形.您可以使用`CGContextSetFillColorWithColor`和`CGContextFillPath`来填充`CGContextAddArcToPoint`绘制的路径. (2认同)
  • +50用于指出控制状态的位掩码潜力,即:`forState:(UIControlStateHighlighted | UIControlStateSelected)`干杯. (2认同)

小智 28

另一种可能性

  1. 在"接口"构建器中创建UIButton.
  2. 给它一个'Custom'类型
  3. 现在,在IB中可以改变背景颜色

但是,按钮是方形的,这不是我们想要的.创建一个带有此按钮引用的IBOutlet,并将以下内容添加到viewDidLoad方法:

[buttonOutlet.layer setCornerRadius:7.0f];
[buttonOutlet.layer setClipToBounds:YES];
Run Code Online (Sandbox Code Playgroud)

不要忘记导入QuartzCore.h

  • 完美,谢谢!我不确定是不是因为我正在使用的iOS版本(5.1)但是setClipToBounds不可用.相反,我使用了buttonOutlet.layer.masksToBounds = TRUE; (7认同)

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)


Jac*_*ngs 7

彩色UIButton

如果您不想使用图像,并希望它看起来与圆角矩形样式完全相同,请尝试此操作.只需在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/上找到了这个.谢谢克里斯!


dub*_*eat 5

好吧,我百分之九十九,你不能只是去改变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)

  • 你是绝对正确的.这是一种痛苦,需要花费几分钟而不是创造图像的麻烦.另外,如果你的按钮是圆形的,那么它就更难以做到......这样的痛苦. (5认同)