views:

2896

answers:

2

I've been developing an iPhone app that uses a UIToolbar (in the context of a UINavigationController) to display a small status icon at the bottom of the screen. The toolbar will eventually also have action icons associated with it (think Mail application).

I'm running into an issue where it appears that each UIViewController pushed onto the navigation controller's stack is expected to have its own set of items for the toolbar to display, and as a result, the "status" item fades out and back in for each view transition.

Is there a way to have a single persistent item in the toolbar? I also tried adding the item in the navigation controller's initializer (I subclassed UINavigationController for this approach), but it's still no go.

A: 

As you push new views onto your navigation controller the views in the toolbar will be replaced with the views from the toolbar of the view on the top of the stack.

Even if you have a static view (a view that does not change when you push a new controller), the view will still appear to be new because of the animation apple includes when you push a new view controller onto the stack.

zPesk
But that sliding view animation doesn't cover the toolbar - the only animation I have to deal with is the fade in/out of [UIToolbar setItems:animated:]. Apple themselves seem to have defeated this in Mail - watch the Refresh item as you transition from a list of emails to a single email.
Tim
if the animation doesnt cover the toolbar then the items shouldn't change right???
zPesk
But each view controller holds its own set of items in the toolbar (or the navigation controller differentiates the sets of items on a per-view-controller basis). So whenever I transition, the _view_ doesn't transition over the toolbar, but the _items_ in the toolbar "change" - even if the two sets of items happen to be the same. This causes the fading effect I'm trying to avoid.
Tim
+7  A: 

Instead of using the navigation controller's toolbar, add one directly to the window and resize the navigation controller's view's frame to avoid it. That single global toolbar will then always be visible.

If you're using the Navigation-based Application template, and are using Interface Builder, the following steps should do it:

  1. Open up your app delegate's .h file.
  2. Add an IBOutlet UIToolbar * toolbar; to the app delegate's instance variables.
  3. Switch to the .m file.
  4. Find the line that reads [window addSubview:[navigationController view]]; and add after it:
    CGRect frame = navigationController.view.frame;
    frame.size.height -= toolbar.frame.size.height;
    navigationController.view.frame = frame;
  5. Add code to release toolbar in the -dealloc method.
  6. Open MainWindow.xib.
  7. Open the window.
  8. Drag a toolbar onto the bottom of the window.
  9. Connect the toolbar to the app delegate's toolbar outlet
  10. Now set up the toolbar—add whatever items you need to it, then create whatever outlets and actions you need in the app delegate and connect them.

Since the toolbar is part of the window, not part of the navigation controller, the navigation controller shouldn't touch it.

Brent Royal-Gordon
OK, but how would this approach affect my ability to add or remove buttons on a per-view-controller basis? I'd still like to have that ability.
Tim
Good answer Brent, that would have been my answer too. Tim, you can still customise the toolbar per view if you need to - when the main view changes, tell the toolbar which buttons to show and which to hide.
h4xxr
Alternatively Tim, use Brent's principle to just add a button (or a segmented control etc etc). Then you have a button appearing "above" the toolbar which always stays there until you hide it.I used this approach to add a segmented control to the bottom toolbar (You're normally only able to add it to the top section of a navigation controller), so I can verify it works exactly as you need.
h4xxr
Great, thanks Brent/h4xxr!
Tim
did this really work for you guys? it looks like my tableView gets under the navigationBar... any ideas?
leolobato