UINavigationBar with back button. Create with XIB

My question is the following: In the interface builder, I am creating a UINavigationBar and I want to create a back button element, but I cannot see any button.

I am using this code:

    UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] init];
    myBarButtonItem.title = @"Back";
    mynavBar.backItem.backBarButtonItem = myBarButtonItem;


mynavBar is my IBOutlet.

Thanks for the help!


source to share

3 answers

You can use the navigation bar as a stand-alone control or in conjunction with a navigation controller. When you use the navigation bar as a stand-alone control, you use the navigation item (an instance of the UINavigationItem class) to specify which buttons or custom views you want to display.

So, in your case, you would use something like this:

UIBarButtonItem *myBarButtonItem = [[[UIBarButtonItem alloc] init] autorelease];
myBarButtonItem.title = @"Back";

UINavigationItem *right = [[[UINavigationItem alloc] initWithTitle:@"Hello!"] autorelease];
right.leftBarButtonItem = myBarButtonItem;

[mynavBar pushNavigationItem:right animated:YES];


You might want to look into UINavigationViewController .



If you want to use a custom button on the left, use mynavBar.leftBarButtonItem instead of backItem. The BackItem will only show up after you have presented another view manager via pushViewController:

. (If you haven't installed your own backbone, then by default a backButton with the title of the previous viewController will be created automatically.)

// edit: you may be looking for this: Draw a custom back button on the iPhone navigation bar



I think this is really how Apple wants it to be implemented.

Place the UINavigationBar

Set output to UINavigationItem

It is a trap

Override navigationItem property to return the generated UINavigationItem.

What is it.

-(UINavigationItem *) navigationItem
    return self.navigationItem1;


If your nav is still in the UINavigationBar, I think you also need a powerful output to the UINavigationBar. Please correct me if I am wrong.



All Articles