为正常/按下和活动状态创建具有背景颜色的按钮

时间:2014-08-01 07:16:31

标签: ios uibutton state

我是iOS开发新手,想要创建一个按钮,根据以下状态更改背景颜色

  1. 正常 - #00aa6e
  2. 按下 - #009762
  3. 有效 - #009762
  4. 我可以不使用任何图像吗?我的应用程序中有8种类型的按钮,这会导致使用这么多图像增加资产大小。

4 个答案:

答案 0 :(得分:2)

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIColor * color1 = (UIColor *)UIColorFromRGB(0xFF00FF)
    ...// set up ur background color here

    [button setBackgroundImage:[self imageWithColor:color1] forState:UIControlStateNormal];
    [button setBackgroundImage:[self imageWithColor:color2] forState:UIControlStateHighlighted];
    [button setBackgroundImage:[self imageWithColor:color3] forState:UIControlStateSelected];
    [button addTarget:self action:selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];   
}

 + (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

- (IBAction)btnSelected:(id)sender {
    [sender setSelected:YES];
}

setBackgroundImage会根据您按钮的大小调整背景图片的大小,您无需为按钮调整图片大小

答案 1 :(得分:1)

我相信你可以。您可以找到答案here

UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
[loginButton setBackgroundColor:[UIColor blueColor]];
[loginButton addTarget:self action:@selector(buttonHighlight:) forControlEvents:UIControlEventTouchDown];
[loginButton addTarget:self action:@selector(buttonNormal:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)buttonHighlight:(UIButton *)sender {
    sender.backgroundColor = (UIColor *)UIColorFromRGB(0xFF00FF);
}

- (void)buttonNormal:(UIButton *)sender {
    sender.backgroundColor = UIColorFromRGB(0x00FFFF);
}

Apple不支持0xRRGGBB录制方法。如果您想将颜色用作十六进制,可以使用HexColors library或使用this中的解答:

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

现在你只需要结合这两个解决方案......还有一件事:如果你不想避免将大图像作为背景颜色,你可以使用一种模式:

UIColor *col = [UIColor colorWithPatternImage:<#(UIImage *)#>];

或线性渐变:

iPhone iOS how to add linear gradient to a UIButton under the button's text or image?

答案 2 :(得分:1)

试试这个

 -(void)viewDidLoad
 {
 [super viewDidLoad];

 UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(20,50 , 50, 50)];
 [myButton setBackgroundColor: [UIColor greenColor]];
 [myButton addTarget:self action:@selector(pressed:) forControlEvents:UIControlEventTouchDown];
 [self.view addSubview:myButton];
 [myButton addTarget:self action:@selector(selected:) forControlEvents: (UIControlEventTouchUpInside)];
 }

 -(void)didReceiveMemoryWarning
 {
 [super didReceiveMemoryWarning];
 // Dispose of any resources that can be recreated.
 }
 -(IBAction)pressed:(id)sender{
  UIButton *myButton = (UIButton*)sender;
 [myButton setBackgroundColor:[UIColor blueColor]];

 }
  -(IBAction)selected:(id)sender{
 UIButton *myButton = (UIButton*)sender;
 [myButton setBackgroundColor:[UIColor redColor]];
 }

这里

绿色 - 正常, 蓝色 - 按下, 红色 - 选中。

希望这有帮助

答案 3 :(得分:0)

@Wilson L回答完美无缺。

对于那些使用Swift进行编码的人,这里是代码:

func imageFromColor(color:UIColor) -> UIImage{
    var rect:CGRect  = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    var context:CGContextRef  = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, color.CGColor );
    CGContextFillRect(context, rect);

    var image:UIImage  = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

    backButton.setBackgroundImage(imageFromColor(UIColor(red: 8/255, green: 75/255.0, blue: 122/255.0, alpha: 1.0)), forState: .Normal)
    backButton.setBackgroundImage(imageFromColor(UIColor(red: 255/255, green: 60/255.0, blue: 93.0/255.0, alpha: 1.0)), forState: .Selected)