Search code examples
ioscocoa-touchuinavigationcontrolleruitoolbar

Why shouldn't I modify the UINavigationController's toolbar?


The documentation for the toolbar property in UINavigationController says:

This property contains a reference to the built-in toolbar managed by the navigation controller. Access to this toolbar is provided solely for clients that want to present an action sheet from the toolbar. You should not modify the UIToolbar object directly.

I can understand why I shouldn't modify the toolbar's visibility or items, because UINavigationController provides an interface to do that. But I've added a button that, when tapped, causes the toolbar to animate offscreen. Why shouldn't I do this?

Must I instead create my own ToolbarNavigationController class that replicates everything UINavigationController does with the toolbar just sod, I can do what I want with the toolbar? Seems like a waste of effort when the UINavigationController already does what I want. Why on earth would the docs suggest I so severely limit what I do with it?


Solution

  • Three ways that having moved the navigation bar might confuse it:

    1. If your app can be rotated, does it stay in the right place after rotation? When it returns to the screen, does it animate on from the right place?
    2. If you display a search bar, the navigation bar animates itself off. If it's already been moved manually, does it know where it is?
    3. If you push a viewController with hidesBottomBarWhenPushed set to YES, and return, does the navigationController put its toolbar back where it belongs?

    On the other hand, how about calling UINavigationController setNavigationBarHidden:NO animated:YES - does that do what you want, while letting the navigation controller maintain control of its toolbar?

    Edit: Sorry about misreading. Yes, you probably can do what you're asking, as long as you don't also do anything (like item 3 above) that mean the navigationController moves its toolbar around.

    On the other hand, the behavior you want can also be achieved as follows:

    1. For the viewController with the multiple toolbars, set its hidesBottomBarWhenPushed to YES.
    2. Place all the toolbars that viewController needs on it, and have it take full control of their positions and visibility.

    If the default toolbar has the same layout as the navigationController's own toolbar, this will create the odd visual effect of seeing the same set of toolbar items slide off and then on again, but everything else should work.