5

I have successfully added my logo on my navigation bar with this code;

UIImage *image = [UIImage imageNamed: @"top-logo"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];

self.navigationItem.titleView = imageView;

the problem is my logo's height is 54 pixels, where the navigation bar, as defaults has a height of 44 pixels. Logo was intentionally designed to overflow from the bottom of the navigation bar, but I have to change the bounds of navigation bar to do that and I don't want to run over Apple's Guidelines. But I need the imageView which is the titleView of the navigation item to overflow from the navigation bar.

Besides, for one of my apps I reduced the height of navigation bar, which started to act funny when app goes to background and come back (height started to change back to normal, which caused black background within the navigation bar).

Bartu
  • 2,189
  • 2
  • 26
  • 50

4 Answers4

8

Here is a post with a similar situation. The accepted answer used a UIButton instead of an imageview.

Code from accepted answer:

- (void)viewDidLoad {
  [super viewDidLoad];

  UIButton *logoView = [[[UIButton alloc] initWithFrame:CGRectMake(0,0,85,40)] autorelease];
  [logoView setBackgroundImage:[UIImage imageNamed:@"navBarLogo.png"] forState:UIControlStateNormal];
  [logoView setUserInteractionEnabled:NO];

  self.navigationItem.titleView = logoView;
}

Image instead of title in navigation bar of TTLauncherView

Community
  • 1
  • 1
propstm
  • 3,461
  • 5
  • 28
  • 41
4

First, in your -viewDidLoad method, create an UIImageView that fits the NavigationBar size. After that you put your logo inside that UIImageView and set it to be your titleView. The code is below:

UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,40,40)];
image.contentMode = UIViewContentModeScaleAspectFit;
[image setImage: [UIImage imageNamed:@"logo"]];
self.navigationItem.titleView = image;
Maurício Fonseca
  • 1,006
  • 1
  • 9
  • 9
0

I was looking for a similar solution, but I tried something a little different and worked perfectly:

-(void)viewDidLoad
{

  [super viewDidLoad];

  //Set the place where you want your logo to appear on your navigation bar (just an example):

  UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(130, 20, 50,  54)];
  imageView.image = [UIImage imageNamed:@"yourImage@2x.png"];
  [self.navigationController.view addSubview:imageView];

}
khr055
  • 28,690
  • 16
  • 36
  • 48
scollaco
  • 947
  • 8
  • 13
0

If you're just looking for a way to add an image to your navigationbar and you're using Swift, this is the code you need:

override func viewDidLoad() {
    super.viewDidLoad()

    let imageView = UIImageView(image: UIImage(named: "your_logo"))
    navigationItem.titleView = imageView
}
Antoine
  • 23,526
  • 11
  • 88
  • 94