1.14. Displaying an Image on a Navigation Bar
Problem
You want to display an image instead of text as the title of the current view controller on the navigation controller.
Solution
Use the titleView property of
the view controller’s navigation item:
-(void)viewDidLoad{[superviewDidLoad];/* Create an Image View to replace the Title View */UIImageView*imageView=[[UIImageViewalloc]initWithFrame:CGRectMake(0.0f,0.0f,100.0f,40.0f)];imageView.contentMode=UIViewContentModeScaleAspectFit;/* Load an image. Be careful, this image will be cached */UIImage*image=[UIImageimageNamed:@"Logo"];/* Set the image of the Image View */[imageViewsetImage:image];/* Set the Title View */self.navigationItem.titleView=imageView;}
Note
The preceding code must be executed in a view controller that is placed inside a navigation controller.
I have already loaded an image into my project’s assets group and I’ve called this image “Logo”. Once you run this app with the given code snippet, you’ll see something similar to that shown in Figure 1-35.

Figure 1-35. An image view in our navigation bar
Discussion
The navigation item of every view controller can display two different types of content in the title area of the view controller to which it is assigned:
Simple text
A view
If you want to use text, you can use the title property of the navigation item. However, if you want more control over the ...
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