Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding barButtonItems to a UINavigationBar without a Navigation Controller

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?

like image 293
JMD Avatar asked Dec 10 '25 20:12

JMD


1 Answers

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]];
like image 190
rdelmar Avatar answered Dec 12 '25 08:12

rdelmar