如何设置UIButton Type Rounded Rect Button的背景颜色?

时间:2013-04-15 20:17:42

标签: iphone ios uibutton

我尝试了tintColor = [UIColor redColor]并完成了工作,但它只显示按下按钮时的预期颜色。

Coloration of UIButtons of type UIButtonTypeRoundedRect他们回答同样的问题,但你需要一个图像来实现它,有没有办法在没有图像的情况下做到这一点?

  • 这就是我想要的:image

  • 以下是我使用图片的方式:image

记住...... 没有图片!

3 个答案:

答案 0 :(得分:2)

使用图像是一种方法,但我想你已经清楚在你的问题中你不想使用图像。这是我创建的按钮,并以编程方式着色。

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100,50);
[btn setTitle:@"Hello" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor colorWithRed:128.0/255.0f green:0.0/255.0f blue:0.0/255.0f alpha:0.7]];
btn.frame = CGRectMake(100.0, 100.0, 120.0, 50.0);//width and height should be same value
btn.clipsToBounds = YES;

btn.layer.cornerRadius = 20;//half of the width
btn.layer.borderColor=[UIColor redColor].CGColor;
btn.layer.borderWidth=2.0f;

[self.view addSubview:btn];

您可以随意更改[UIColor ColorWithRed: wo,甚至可以使用[UIColor redColor]或其他任何您想要做的事情。

这是一个截图:

enter image description here

无法在圆角按钮中添加背景颜色,将按钮类型更改为自定义,然后使用石英芯设置圆形按钮。所以将它添加到您的项目并将其导入.m,它应该工作。希望这适合你。

将按钮更改为与您显示的图像类似的内容,将框架更改为:

btn.frame = CGRectMake(50.0, 100.0, 200.0, 50.0);

并将半径更改为:

btn.layer.cornerRadius = 7;

应该这样做。

答案 1 :(得分:1)

您可以创建一种方法,从UIImage生成UIColor,然后将返回的图像设置为按钮的背景,例如:

- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);

    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}


UIImage *image = [self imageWithColor:[UIColor colorWithRed:151.0/255.0
                                                      green:36.0/255.0
                                                       blue:40.0/255.0
                                                      alpha:1.0]];
[self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.button setBackgroundImage:image forState:UIControlStateNormal];
self.button.layer.cornerRadius = 6.0;
self.button.layer.borderWidth = 1.0;
self.button.layer.borderColor = [UIColor darkGrayColor].CGColor;
self.button.clipsToBounds = YES;

使用这个我得到

enter image description here

答案 2 :(得分:0)

这是我应该怎么做的:

从互联网下载按钮图像或自己创建图像,使其看起来像你想要的样子。 Photoshop可能是你最好的选择。确保您拥有透明背景并将其另存为.png。 然后将其添加到您的xcode项目中。制作按钮:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(buttonAction)
         forControlEvents:UIControlEventTouchDown];
    [btn setTitle:@"Use it!" forState:UIControlStateNormal];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    btn.titleLabel.font = [UIFont systemFontOfSize:20];
    btn.frame = CGRectMake(position and size of your button);
    [self.view addSubview:btn];

yourImage.png需要替换为图片名称。

buttonAction是您的按钮点击时执行的操作。如果你只是添加:

- (void) buttonAction
{

}

您文件中的任何位置,都会执行您在这些括号中添加的任何内容。

确保按钮的大小与CGRect的大小相同,以确保它看起来不舒展或者您不希望它看起来像什么。

如果您有任何疑问,请随时向他们提问。

希望这有助于你