views:

206

answers:

2

I can create a button like this, and the action works fine:

UIBarButtonItem *myBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"image.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(myMethod:)];

However, when tapping the button on screen, no Down state highlight is displayed. I've tried everything. Can you not get a highlight on a UIBarButtonItem with a custom image?

+2  A: 

Try something like this (setShowsTouchWhenHighlighted: is what you're looking for):

UIImage* image = [UIImage imageNamed:@"image.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton* button = [[UIButton alloc] initWithFrame:frame];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(myMethod:) forControlEvents:UIControlEventTouchUpInside];
[button setShowsTouchWhenHighlighted:YES];
UIBarButtonItem* barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItem:barButtonItem];
[barButtonItem release];
[button release];
Shaggy Frog
That works, but I need it to be of style UIBarButtonItemStyleBordered. And I need the highlight of just darkening a little, like a typical button in a nav bar.
sol
FWIW, I use this code for "a typical button in a nav bar".
Shaggy Frog