简体   繁体   中英

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.

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.

Class to create button images:

Class to create button with button images:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM