How to put an image as the navigation bar title

Iphone

Iphone Problem Overview


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.

Iphone Solutions


Solution 1 - Iphone

Set the navigationItem's titleView

UIImage *image = [UIImage imageNamed:@"image.png"];
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:image];

Solution 2 - Iphone

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

Solution 3 - Iphone

You can change all the views in a UINavigationBar. If you are trying to alter the navigationBar in a navigation controller do:

self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"] autorelease];

If you are creating the navigationBar yourself do the same, but in stead of calling self.navigationItem call: navigationBar.topItem

Solution 4 - Iphone

In case if someone needs both image and text inside the Navigation bar title view in Swift 4.2, please try this function:-

override func viewDidLoad() {
    
    super.viewDidLoad()
    
    //Sets the navigation title with text and image
    self.navigationItem.titleView = navTitleWithImageAndText(titleText: "Dean Stanley", imageName: "online")
}

func navTitleWithImageAndText(titleText: String, imageName: String) -> UIView {
    
    // Creates a new UIView
    let titleView = UIView()
    
    // Creates a new text label
    let label = UILabel()
    label.text = titleText
    label.sizeToFit()
    label.center = titleView.center
    label.textAlignment = NSTextAlignment.center
    
    // Creates the image view
    let image = UIImageView()
    image.image = UIImage(named: imageName)
    
    // Maintains the image's aspect ratio:
    let imageAspect = image.image!.size.width / image.image!.size.height
    
    // Sets the image frame so that it's immediately before the text:
    let imageX = label.frame.origin.x - label.frame.size.height * imageAspect
    let imageY = label.frame.origin.y
    
    let imageWidth = label.frame.size.height * imageAspect
    let imageHeight = label.frame.size.height
    
    image.frame = CGRect(x: imageX, y: imageY, width: imageWidth, height: imageHeight)
    
    image.contentMode = UIView.ContentMode.scaleAspectFit
    
    // Adds both the label and image view to the titleView
    titleView.addSubview(label)
    titleView.addSubview(image)

    // Sets the titleView frame to fit within the UINavigation Title
    titleView.sizeToFit()
    
    return titleView
    
}

enter image description here

Solution 5 - Iphone

Updated with Swift 2.x

navigationItem.titleView = UIImageView.init(image: UIImage(named:"yourImageName"))

Solution 6 - Iphone

Swift 5 version:

navigationItem.titleView = UIImageView(image: UIImage(named: "logo.png"))

Solution 7 - Iphone

Searching for an answer to this question, I've found a nice and simple solution in the Apple Discussions forum, overriding -(void)drawRect:(CGRect)rect for UINavigationBar :

@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
	UIImage *image = [UIImage imageNamed: @"NavigationBar.png"];
	[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionebaccountView Question on Stackoverflow
Solution 1 - IphoneJason HarwigView Answer on Stackoverflow
Solution 2 - IphonerambossaView Answer on Stackoverflow
Solution 3 - IphoneklaaspieterView Answer on Stackoverflow
Solution 4 - IphoneGagandeep GambhirView Answer on Stackoverflow
Solution 5 - IphoneIan HanView Answer on Stackoverflow
Solution 6 - IphoneRoozbeh ZabihollahiView Answer on Stackoverflow
Solution 7 - IphoneJoseView Answer on Stackoverflow