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.
Asked
Active
Viewed 1,257 times
2
-
1check http://www.cimgf.com/2010/01/28/fun-with-uibuttons-and-core-animation-layers/ – AmineG Oct 08 '11 at 21:06
-
I'm kinda having a problem here, the color will come only after clicking on the button, any advise please. Thanks . -(IBAction) nom{ [[button layer] setCornerRadius:8.0f]; [[button layer] setMasksToBounds:YES]; [[button layer] setBorderWidth:1.0f]; [button setBackgroundColor:[UIColor redColor]]; } – idipro Oct 09 '11 at 00:04
1 Answers
4
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:
- 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
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];

David M. Syzdek
- 15,360
- 6
- 30
- 40