Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put an image as the navigation bar title

Tags:

iphone

I want to put a .png image in the middle of the navigation bar instead of the title written in text. Could you please let me know how to do that?

Thanks.

like image 401
ebaccount Avatar asked May 14 '09 19:05

ebaccount


People also ask

How do you set a navigation item title?

Discussion. Use navigationBarTitle(_:) to set the title of the navigation bar. This modifier only takes effect when this view is inside of and visible within a NavigationView .


2 Answers

Set the navigationItem's titleView

UIImage *image = [UIImage imageNamed:@"image.png"]; self.navigationItem.titleView = [[UIImageView alloc] initWithImage:image]; 
like image 173
Jason Harwig Avatar answered Oct 17 '22 08:10

Jason Harwig


Swift 4.2
Includes suggested framing/scaling

let image: UIImage = UIImage(named: "image.png")! let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40)) imageView.contentMode = .scaleAspectFit imageView.image = image self.navigationItem.titleView = imageView 
like image 27
rambossa Avatar answered Oct 17 '22 07:10

rambossa