Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: Creating Custom UIBarButton that has different color when pressed?

Tags:

iphone

How do I create a custom UIBarButton that changes color when I press it? Right now using the plain style, it just highlights, but to have a more bolder effect, I want it to change color instead. Any pre-existing solution I can use?

like image 966
Henley Avatar asked Jan 01 '26 00:01

Henley


1 Answers

I assume you mention here about UINavigationBarButton. Here what i did and its working perfectly for me. I just tested it again to make sure if it is working or not. And this is working.

UIImage *buttonImage = [UIImage imageNamed:@"Done.png"];
UIImage *buttonImage2 = [UIImage imageNamed:@"DoneSelected.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setImage:buttonImage forState:UIControlStateNormal];
[aButton setImage:buttonImage2 forState:UIControlStateSelected];
[aButton setImage:buttonImage2 forState:UIControlStateHighlighted];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:@selector(navigatehome) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = aBarButtonItem;
[aBarButtonItem release];

Hope this will help you. Thanks :)

like image 127
Sameera Chathuranga Avatar answered Jan 02 '26 16:01

Sameera Chathuranga