3

I am attempting to add a number of buttons to a uiNavigationBar, but it is not attached to a navigation controller, and I don't want it to be.

I am attempting to add two buttons to the left side of the bar, but the only examples of code I find that allow this involve using the line

 self.navigationItem.leftBarButtonItems

obviously, my UINavigationBar is not a navigatinItem.

Here is how I have created my NavBar..

.h

@property (nonatomic, retain) UINavigationBar *navBar;

.m

_navBar = [[UINavigationBar alloc] init];
[_navBar setFrame:CGRectMake(0,0,self.view.bounds.size.width,52)];
[self.view addSubview:_navBar];

I have seen two ways to push items to a nav bar but neither work.

first being..

    UIBarButtonItem *barButton = [[UIBarButtonItem alloc]
                                  initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                  target:self
                                  action:@selector(buttonSettingClicked)];

    [[self navigationItem] setLeftBarButtonItem:barButton]

and

     self.navigationItem.leftBarButtonItems = _ArrayOfBarButtons;

neither one produces any results... i suspect because my UINavigationBar is not technically a 'navigation item.'

So how can I add items to my NavigationBar?

JMD
  • 1,540
  • 2
  • 24
  • 56
  • You can start by reading the "Adding Content to a Navigation Bar" section of the UINavigationBar Class reference. As it says, you need to add your buttons to an instance of a UINavigationItem. In your code, you reference self.navigationItem, but you never create one. – rdelmar Jan 24 '13 at 17:28

2 Answers2

5

You need to create the UINavigationItem first, add the button to it, and then add the navigationItem to the navigation bar.

[super viewDidLoad];
    _navBar = [[UINavigationBar alloc] init];
    [_navBar setFrame:CGRectMake(0,0,self.view.bounds.size.width,52)];
    [self.view addSubview:_navBar];
    UINavigationItem *navItem = [[UINavigationItem alloc]initWithTitle:@""];
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(buttonSettingClicked)];
    [navItem setLeftBarButtonItem:barButton];

    [_navBar setItems:@[navItem]];
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Oh, ok, so its as simple as then then... Thanks for the help! – JMD Jan 24 '13 at 18:02
  • @JesseDurham, yep, it is. If you want multiple buttons, just create more of them, use setLeftBarButtonItems:, and pass an array of your buttons. – rdelmar Jan 24 '13 at 18:04
0
  m_navBar = [[UINavigationBar alloc] init];
  [m_navBar setFrame:CGRectMake(0,0,self.view.bounds.size.width,52)];
  [self.view addSubview:m_navBar];

  UIBarButtonItem *barButton = [[UIBarButtonItem alloc]
                                initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                target:self
                                action:@selector(buttonSettingClicked)];


  UINavigationItem* navItem = [[UINavigationItem alloc] initWithTitle:@"MyItem"];
  [m_navBar pushNavigationItem:navItem animated:NO];

  navItem.leftBarButtonItems = @[barButton, barButton];
DaNY
  • 191
  • 2
  • 6