views:

172

answers:

1

I have a UINavigationController where I initialize the NavigationBar as such (using MonoTouch):

NavigationBar.BarStyle = UIBarStyle.Black;
NavigationBar.TintColor = UIColor.Black;

On a UIViewController that I subsequently push onto the navigation controller, I add a button as such:

NavigationItem.RightBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Add, (s, e) => HandleAddItem());

However, when I touch the button, it doesn't change color/shade (i.e. animate) to signify it's been touched. Similar UIBarButtonItems added to a manually-created UINavigationBar on another view controller (plain UIVIewController) animate as I'd expect.

How can I get the navigation bar buttons to "flash" when they are on a UIViewController that has been pushed onto a UINavigationController?

+2  A: 

I have found that once you change the UINavigationBar's barStyle and tintColor, there is a very good chance that the highlighted state for the button will be no different than the default state. I believe the best way to work around this is to use a UIBarButtonItem created from a custom view.

UIImage *bgImage = [UIImage imageNamed:@"button-normal.png"];
bgImage = [bgImage stretchableImageWithLeftCapWidth:6.0 topCapHeight:0.0];
UIImage *bgImageHighlighted = [UIImage imageNamed:@"button-highlighted.png"];
bgImageHighlighted = [bgImageHighlighted stretchableImageWithLeftCapWidth:6.0 topCapHeight:0.0];

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
[myButton setBackgroundImage:bgImageHighlighted forState:UIControlStateHighlighted];

UIBarButtonItem *myItem = [[UIBarButtonItem alloc] initWithCustomView:myButton];

It's kind of a pain, especially when your button uses an image instead of a title. I would definitely recommend submitting a bug report to Apple about this.

In your particular situation, it might work better to only set the barStyle to UIBarStyleBlack and leave the tintColor property alone. I have also had luck only specifying the tintColor and leaving barStyle set to UIBarStyleDefault, as long as the tintColor is not black. In general, a tintColor of black does not work very well.

Sebastian Celis
It times like this when I love SO. Removed tintColor setting and voila! Magic. Many thanks.
dommer