rightBarButtonItem does not appear in Navigation Bar iOS

17,080

You need to attach your bar button item to your custom view controller, not to the navigation controller. From Updating the Navigation Bar:

In addition, the navigation controller object builds the contents of the navigation bar dynamically using the navigation items (instances of the UINavigationItem class) associated with the view controllers on the navigation stack. To change the contents of the navigation bar, you must therefore configure the navigation items for your custom view controllers.

(...)

The navigation controller updates the right side of the navigation bar as follows:

  • If the new top-level view controller has a custom right bar button item, that item is displayed. To specify a custom right bar button item, set the rightBarButtonItem property of the view controller’s navigation item.

  • If no custom right bar button item is specified, the navigation bar displays nothing on the right side of the bar.

Therefore, replace:

self.navController.navigationItem.rightBarButtonItem = barButton;

with:

list.navigationItem.rightBarButtonItem = barButton;
Share:
17,080
Russell
Author by

Russell

Updated on June 18, 2022

Comments

  • Russell
    Russell almost 2 years

    I'm having problems displaying the rightBarButtonItem of the Navigation Bar - I'm attempting to create it programmatically in the Application Delegate, where my UINavigationController is set up.

    Code is as follows:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    // Override point for customization after application launch.
    RSCListViewController *list = [[RSCListViewController alloc] initWithStyle:UITableViewStylePlain];
    self.navController = [[UINavigationController alloc] initWithRootViewController:list];
    
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"+"
                                                                  style:UIBarButtonItemStylePlain
                                                                 target:list
                                                                 action:@selector(addPressed:)];
    
    self.navController.navigationItem.rightBarButtonItem = barButton;
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    [DatabaseManager openDatabase];
    
    return YES;
    }
    

    Running the application, no button item appears on the navigation bar.

    I'm not sure whether I have missed something obvious - my attempts to rectify the problem using related Stack Overflow threads haven't yielded any success.

    Any help appreciated.

  • Russell
    Russell over 11 years
    Thanks - had assumed that the elements displayed on the navigation bar were tied to the navigation controller rather than the view at the top of the stack... but now I think about it, this wouldn't make much sense as the navigation bar will often change as a result of which view is being displayed. Thanks again for the help.
  • Anton Tropashko
    Anton Tropashko almost 7 years
    For some reason that does not work when the VC whose navigationItem you set rightBarButtonItem is the rootViewController :-( But, as you said, it works fine whenever the vc with @navigationItem.rightBarButtonItem = whatever;" is pushed onto nav stack though.
  • Dani
    Dani over 5 years
    What is list?
  • albertamg
    albertamg over 5 years
    @DanielSpringer a custom view controller (RSCListViewController). Read the code snippet in the question
  • Dani
    Dani over 5 years
    So in the view controller file itself I should use the class name? That’s quite unusual. For now, what worked for me was to replace self.navController.navigationItem.rightBarButtonItem = barButton with navigationItem.rightBarButtonItem = barButton - but I don’t know why that worked. Is it the same as MyViewController.navigationItem.rightBarButtonItem = barButton ?
  • albertamg
    albertamg over 5 years
    No, you should not use the class name. The variable list refers to an instance of the class. In the question, the rightBarButtonItem is set from outside the class.