O'Reilly logo

iOS 6 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

2.15. Adding Buttons to Navigation Bars Using UIBarButtonItem

Problem

You want to add buttons to a navigation bar.

Solution

Use the UIBarButtonItem class.

Discussion

A navigation bar can contain different items. Buttons are often displayed on the left and the right sides. These buttons are of class UIBarButtonItem and can take many different shapes and forms. Let’s have a look at an example in Figure 2-41.

Different buttons displayed on a navigation bar

Figure 2-41. Different buttons displayed on a navigation bar

You might be surprised that the bar on the bottom of Figure 2-41 is also a navigation bar! Navigation bars are of class UINavigationBar and can be created at any time and added to any view. So just look at all the different buttons with different shapes that have been added to the navigation bars in Figure 2-41. The ones on the top right have up and down arrows, the one on the top left has an arrow pointing to the left, and the ones on the bottom navigation bar have all sorts of shapes. We will have a look at creating some of these buttons in this recipe.

Note

For this recipe, you must follow the instructions in Recipe 1.1 to create an Empty application. Then follow the instructions in Recipe 2.12 to add a navigation controller to your app delegate.

In order to create a navigation button, we must:

  1. Create an instance of UIBarButtonItem.

  2. Add that button to the navigation bar of a view controller using the view controller’s navigationItem ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required