【问题标题】:How to set the background color of an UIButtonTypeRoundedRect Button?如何设置 UIButton 类型 RoundedRect 按钮的背景颜色?
【发布时间】:2013-04-15 20:17:42
【问题描述】:

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

Coloration of UIButtons of type UIButtonTypeRoundedRect 他们回答了同样的问题,但是你需要一个图像来实现它,没有图像有什么办法吗?

  • 这是我想要的:image

  • 这是我使用图像得到的结果:image

记住...没有图像!

【问题讨论】:

  • @Balz,检查我编辑的答案。那应该可以解决您的问题。如果您不想使用边框,只需删除它,它就只有一种颜色。
  • 检查我的答案中的编辑。

标签: iphone ios uibutton


【解决方案1】:

使用图像是一种方法,但我想您已经在您的问题中明确表示您不希望使用图像。这是我以编程方式创建和着色的按钮。

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] 或任何其他您想做的事情。

这是一个屏幕截图:

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

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

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

并将半径更改为:

btn.layer.cornerRadius = 7;

应该这样做。

【讨论】:

  • 有什么办法让它看起来像image
  • 你可以玩转角半径的大小。减少它直到你得到想要的效果。石英芯一切皆有可能。随便玩玩,用我写的代码做一些你喜欢的东西。
【解决方案2】:

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

- (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;

用这个我得到

【讨论】:

  • 嗨,这产生了this...但我需要this之类的东西。
  • 是的,我明白了,我已经编辑了我的答案,检查按钮配置
  • 有没有办法给按钮的颜色添加一个垂直渐变??...看看这个按钮image of button ...它的底部有一个更深的红色和一个更浅的红色顶部的颜色。
  • 是的,有可能,创建一个图像,子类 UIButton 并覆盖 draw 方法,或者从这里阅读所有解决方案google.es/search?q=gradient+background+uibutton
  • 我找到了这个article,你应该看看它
【解决方案3】:

我应该这样做:

从互联网下载按钮图像或自己创建图像,使其看起来像您想要的样子。 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 的大小相同,以确保它看起来没有被拉长或任何您不希望它看起来像的东西。

如果您有任何问题,请随时提出。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 2011-06-13
    相关资源
    最近更新 更多