Question

This might be a small question, but how do you have a title bar with UIBarButtonItems in a view that is embedded in a UITabBarController? For example, in the Apple Clock App, you see a title bar showing "Stopwatch" as the title for this tab. In other tabs there are UIBarButtonItems up there as well.

Image of Apple Clock App

According to this Apple GettingStarted Guide, the top controls are UIBarButtonItems, like on the World Clock page. One puts it into edit mode, and the other opens a Modal dialog to select the location. Neither of which are push segue navigations, which I thought were mainly what is used in UINavigationControllers

I know that I could embed the main view in a UINavigationController to give the Navigation Bar, but that seems like a lot of overhead for a bar. I could add a UIToolbar up there, but according to this Apple Technical Q&A you should not do that:

Note: For clarification, on iPhone, a toolbar should always appear at the bottom edge of a screen or view, but on iPad it can appear at the top edge.

For my app, I wanted a sharing button on the top right for at least one of the tabs, so it would load a modal Activity View Controller, so it is not strictly speaking a navigation (no push segue's like one normally does in a UINavigationController. So while I CAN use a UINavigationControlller, it seems like unnecessary overhead for something that won't be navigating.

So, how is the best way to do add that bar for a title and UIBarButtonItems to a view controller embedded in a UITabBarController?

Was it helpful?

Solution

While Apple's documentation says UIToolbars should always appear near the bottom, you technically can place them at the time and your app will still be accepted. Many apps do use a toolbar at the top of the screen.

Best way

Use a UINavigationController.

It is not a lot of overhead and it has more advantages in the long run than a UIToolbar.

Advantages:

  • You get a title placed on the top bar (UINavigationBar) for free. (No need to create a UIBarButtonItem or UILabel for that.)
  • In the future, if you decide you do want a segue, you won't have to refactor and put a navigation controller in there. (Extensibility is a good coding practice to get in the habit of.)

tl;dr - Feel free to use a UIToolbar if you want something simple. Use a UINavigationController if you want to do it the "right" way and want to follow Apple's guidelines.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top