更改UIButton背景颜色
问题描述:
有人可以请帮助我如何将按钮背景从白色更改为不同的颜色。 我知道如何添加图像并使其成为自定义按钮,但我不知道如何将初始按钮颜色从白色更改为其他颜色。 您的帮助将不胜感激。干杯。更改UIButton背景颜色
答
我有一个类似的要求,并不想在我的应用程序包中包含额外的文件,所以我写了一个从RGBA值生成按钮图像并将它们添加到UIButton的两个类。
类创建按钮图像:
- https://github.com/bindle/BindleKit/blob/master/BindleKit/views/BKButtonImages.h
- https://github.com/bindle/BindleKit/blob/master/BindleKit/views/BKButtonImages.m
类具有按钮图像来创建按钮:
- https://github.com/bindle/BindleKit/blob/master/BindleKit/views/iOS/BKButton.h
- https://github.com/bindle/BindleKit/blob/master/BindleKit/views/iOS/BKButton.m
如果添加了上述四个文件到你的项目,你应该能够使用以下方法与彩色背景创建的UIButton:
UIButton * deleteButton = [BKButton redButton];
UIButton * selectButton = [BKButton lightGrayButton];
UIButton * customButton = [BKButton buttonWithRed:0.0 green:0.4 blue:0.37 alpha:1.0];
+0
非常感谢。 – idipro
检查http://www.cimgf.com/ 2010/01/28/fun-with-uibuttons-and-core-animation-layers/ – AmineG
非常感谢! – idipro
我在这里有点问题,只有在点击按钮后才会出现颜色,请提出任何建议。谢谢 。 - (IBAction)nom {[button layer] setCornerRadius:8.0f]; [[button layer] setMasksToBounds:YES]; [[button layer] setBorderWidth:1.0f]; [button setBackgroundColor:[UIColor redColor]]; } – idipro