Daniel
Daniel

Reputation: 285

Adding Bar Button Item

problem

I cannot for the life of me get it to show.

Here's the implementation of the nav bar

_navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
[_navBar setDelegate:self];
[self.view addSubview:_navBar];

_navBar is a UINavigationBar property.

Here's where I add the button.

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                        target:self
                                                                        action:@selector(flipView:)];
self.navigationItem.rightBarButtonItem = button;

However, nothing shows. Any help?

Upvotes: 0

Views: 240

Answers (1)

matt
matt

Reputation: 534885

self.navigationItem is read automatically by a parent UINavigationController. But you don't have one.

Thus, you have two choices:

  • Instead of a loose nav bar that you create and put into the interface yourself (as you are doing in your first code), be the child of a UINavigationController. Now setting self.navigationItem will work.

  • Or, create a navigation item, configure it, and push it manually onto your loose nav bar. Your code will then have this structure:

    UINavigationItem* ni = [[UINavigationItem alloc] initWithTitle:// ...];
    UIBarButtonItem* b = // ...;
    ni.rightBarButtonItem = b;
    _navbar.items = @[ni];
    

Upvotes: 1

Related Questions