I wanted to add a quick post about this as it was something which took me a while to figure out and so I hope it will be helpful to others.

Sometimes in an app. you want a UIButton to be pressed and remain selected. Now you can set the selected property to YES
, but this doesn’t keep the appearance of a selected button.

Now you could make a custom image for your button and use :

[button setBackgroundImage:[UIImage imageNamed:@”someimage.png”] forState:UIControlStateSelected];

However I found this way of doing it to be better, particularly, as with most coders, if you’re not a great fan on photoshop.

first add #import <UIKit/UIKit.h>

We can then use the graphics layer to alter the buttons appearance. My thanks to http://cocoawithlove.com/ for pointing me in the right direction for this one:

- (void)controlButton:(UIButton *)sender
{

[[sender layer] setCornerRadius:8.0f];
[[sender layer] setMasksToBounds:YES];
[[sender layer] setBorderWidth:1.0f];

if (sender.selected == YES) {

[[sender layer] setBackgroundColor:[[UIColor lightGrayColor] CGColor]];

}else{
[[sender layer] setBackgroundColor:[UIColor whiteColor].CGColor];
}

}

© 2012 Craig Brookes Suffusion theme by Sayontan Sinha