Set back button to empty string in iOS

I unwillingly spent a bit of time this evening trying to make the back navigation button show up without text.

There are many people that suggest setting the following in loadView or viewDidLoad

self.navigationController.navigationBar.topItem.title = @"";  

This will work, but it seemed very clunky to me.

After doing some reading on the subject it finally clicked. The navigation framework uses UINavigationItem to dictate how the navigation bar is rendered.

The managing UINavigationController object uses the navigation items of the topmost two view controllers to populate the navigation bar with content.

The backBarButtonItem property of a navigation item reflects the back button you want displayed when the current view controller is just below the topmost view controller. In other words, the back button is not used when the current view controller is topmost.

Ah ha! In a controller, you configure how the back button will display when that controller becomes the background controller.

You can then add to loadView or viewDidLoad the following:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];  

This will cause the back button to show an arrow when you push another controller onto the navigation stack.

The main point is that you configure the backBarButtonItem properties on a controller for its future use on the navigation stack. Not the current.

comments powered by Disqus