bbrame bbrame - 1 year ago 121
iOS Question

Add a left bar button item to UINavigationController when no back button is present

I'd like to add a default left bar button item to my navigation bar. It should only display when there is no back button supplied by the UINavigationController.

What is the best approach?

Jef Jef
Answer Source
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if(navigationController.viewControllers.count != 1) { // not the root controller - show back button instead
    UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
    [viewController.navigationItem setLeftBarButtonItem:menuItem];