2.11. 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-32.

Figure 2-32. Different buttons displayed on a navigation bar
You might be surprised that the bar on the
bottom of Figure 2-32 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-32. 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.8 to add a navigation controller to your app delegate.
In order to create a navigation button, we must:
Create an instance of
UIBarButtonItem.Add that button to the navigation bar of a view controller using the view controller’s
navigationItem ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access