1.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 1-36.

Figure 1-36. Different buttons displayed on a navigation bar
Navigation bars are of class UINavigationBar and can be created at any time
and added to any view. Just look at all the different buttons with
different shapes that have been added to the navigation bar in Figure 1-36. The ones
on the top right have up and down arrows, and the one on the top left
has an arrow pointing to the left. We will have a look at creating some
of these buttons in this recipe.
Note
For this recipe, you must follow the instructions in Creating and Running Our First iOS App to create an empty application. Then follow the instructions in Recipe 1.12 to add a navigation controller to your app delegate.
In order to create a navigation button, we must do the following:
Create an instance of
UIBarButtonItem.Add that button to the navigation bar of a view controller using the view controller’s
navigationItemproperty. ThenavigationItemproperty allows us to interact with the navigation bar. This property has two others ...
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