From Documentation for Apple :
The most common way to use the navigation bar is through the navigation controller. You can also use the navigation bar as a separate object in your application.
So, if you have a UINavigationController, all you have to do to set the title of the navigation bar (as explained in all previous answers)
self.navigationItem.title = @"title";
But if you have a standalone navigation bar created programmatically inside the UIViewController object, you must set the initial view of the navigation bar by creating the corresponding UINavigationItem objects and adding them to the navigation bar object stack, i.e.
- Create an instance of the navigation bar (UINavigationBar)
- Create an instance of the navigation bar element (UINavigationItem) that controls the buttons and views that will be displayed in the UINavigationBar. Please note that you are setting the title in this step.
- (Optional step) Add right / left buttons (instances of UIBarButtonItem) to the navigation bar item.
Example Objective-C Code for the specified steps:
UINavigationBar* navbar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)]; UINavigationItem* navItem = [[UINavigationItem alloc] initWithTitle:self.shoppingItem.name]; UIBarButtonItem* cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onTapCancel:)]; navItem.leftBarButtonItem = cancelBtn; UIBarButtonItem* doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onTapDone:)]; navItem.rightBarButtonItem = doneBtn; [navbar setItems:@[navItem]]; [self.view addSubview:navbar];
For the Swift standalone navigation bar version, select this answer .
Wael Showair Dec 01 '15 at 17:09 2015-12-01 17:09
source share