bbrame bbrame - 18 days ago 7
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
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if(navigationController.viewControllers.count != 1) { // not the root controller - show back button instead
        return;
    }
    UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
                                                                              target:self
                                                                              action:@selector(menuItemSelected:)];   
    [viewController.navigationItem setLeftBarButtonItem:menuItem]; 
}
Comments