更改UIButton背景颜色 [英] Change UIButton background color
问题描述
有人可以帮助我如何将按钮背景从白色更改为不同的颜色。
我知道如何添加图像并使其成为自定义按钮,但我不知道如何将初始按钮颜色从白色更改为另一种颜色。
我们非常感谢您的帮助。欢呼。
can someone please help me on how to change the button background from white to a different color. I know how to add an image and make it a custom button, but i don't know how to change the initial button color from white to another color. Your help will be much appreciated. cheers.
推荐答案
我有类似的要求,不想在我的应用程序包中包含其他文件,所以我写了两个班从RGBA值生成按钮图像并将它们添加到UIButton。
I had a similar requirement and did not want to include additional files in my app bundle so I wrote a two classes which generate button images from RGBA values and add them to 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/BKButtonImages.h
- https://github.com/bindle/BindleKit/blob/master/BindleKit/views/BKButtonImages.m
使用按钮图像创建按钮的类:
Class to create button with button images:
- https://github.com/bindle/BindleKit/blob/master/BindleKit/views/iOS/BKButton.h
- https:// github .com / bindle / BindleKit / blob / master / BindleKit / views / iOS / BKButton.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:
If you add the above four files to your project, you should be able to create UIButton with colored backgrounds using the following methods:
UIButton * deleteButton = [BKButton redButton];
UIButton * selectButton = [BKButton lightGrayButton];
UIButton * customButton = [BKButton buttonWithRed:0.0 green:0.4 blue:0.37 alpha:1.0];
这篇关于更改UIButton背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!