views:

1183

answers:

3

This may be a stupid question, but is it possible to keep a right UIBarButtonItem across multiple views managed by a UINavigationController? I have a progression of views that often share the same right UIBarButtonItem, but when I push a new view to my UINavigationController, I have to redefine the button every time, even if it hasn't changed. Mostly, the noticeable transitional animation from one view to the next is what bothers me because the exact same button briefly fades out then back in, which is unnecessary since there is no actual visual change. Should I consider just adding a UIButton as a subview of my UINavigationBar and accomplish a "right bar button" effect this way?

A: 

The View Controller Programming Guide is a great place to start.

I think there is a property called 'hides button bar when pushed' but I can't remember where and I'm to lazy to try to find it. If you want something always displayed, it may actually be smarter to have your UINavigationController as a subview to another view like so:

--- view that has button bar ------------------
|  |- view that has navigation controller -|  |
|  |      -uinavigationcontroller-         |  |
|  -----------------------------------------  |
|  |------------ UIButtonBarView-----------|  |
-----------------------------------------------
slf
I think you're thinking of hidesBottomBarWhenPushed, which applies only to a tab bar I believe.
LucasTizma
Ah, yeah I think so
slf
+1  A: 

We did this in our app. We created a single UIBarButtonItem that we swap from one UIViewController to another as the user navigates. The trick was to use UINavigationControllerDelegate, the navigationController:willShowViewController:animated: method. We set the outgoing UIViewController's rightBarButtonItem to nil and the incoming controller's rightBarButtonItem to your button. It's a pain to manage, but it works.

Neil Mix
Awesome! Thank you. I forgot to investigate the UINavigationControllerDelegate.
LucasTizma
+1  A: 

UIBarButtonItems can be created as standalone instances in a nib/xib file. If there's nothing special going on that prevents you from being able to build the button in Interface builder, you should be to connect it to the rightBarButtonItem property of the UINavigationItem for each view controller that needs to reuse the button. No objective c needed.

Hi Matt, Can you be more specific with example -- I believe this is the implementation in the sample code NavBar.xcode, but I am having some trouble following it all the way through. (BTW - I voted for your answer as I think it is both correct and the more elegant solution.)
mobibob