8

I want to put Custom image on my UINavigationBar,

When i tried this two code snippets,

[self.navigationItem setTitleView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]]];

And

    UIButton *logoView = [[UIButton alloc] initWithFrame:CGRectMake(0,0,70,30)];
    [logoView setBackgroundImage:[UIImage imageNamed:@"logo.png"] forState:UIControlStateNormal];
    [logoView setUserInteractionEnabled:NO];
    self.navigationItem.titleView = logoView;

image which i can see on my Device is getting blur or it is getting streched.

I made image with Resolution of 180*40(Width*Height)

I want to know the exact size of Image. what, it would be ?

Thanks in advance.

Krunal
  • 6,440
  • 21
  • 91
  • 155

3 Answers3

22

use this

UIImageView *navigationImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 98, 34)];
navigationImage.image=[UIImage imageNamed:@"topNav-logo.png"];

UIImageView *workaroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 98, 34)];
    [workaroundImageView addSubview:navigationImage];
    self.navigationItem.titleView=workaroundImageView;

hope this helps.

iEinstein
  • 2,100
  • 1
  • 21
  • 32
3

Create Image with size (320 x 44) and set as a Title like bellow..

self.navigationItem.titleView = yourImageView;

See whole example like bellow...

- (void)viewDidLoad{
        UIImageView* imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
        self.navigationItem.titleView = imgView;
        [imgView release];
}
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
1

You need to first initialize the titleView. and then set it the imageView as a subView:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];

self.navigationItem.titleView = [[UIView alloc] initWithFrame:imageView.bounds];
[self.navigationItem.titleView addSubview:imageView];
Lirik
  • 3,167
  • 1
  • 30
  • 31
  • it's working but still image is getting stretched, what should be the resolution of image ? – Krunal Jul 18 '13 at 09:53
  • You can make the image which ever resolution you like, you should check a couple of things though: 1. the image content mode should be aspectFit, so it wont get of ouf bounds when added to superView. 2. check image autoresizing as well (remove streching). 3. self.navigationItem.titleView.autoresizesSubviews is default == YES, try setting it to NO. – Lirik Jul 18 '13 at 10:21
  • How to make image content mode aspectFit ? – Krunal Jul 18 '13 at 10:52
  • imageView.contentMode = UIViewContentModeScaleAspectFit; – Lirik Jul 18 '13 at 10:55
  • I tried this but still facing the problem `UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]]; self.navigationItem.titleView = [[UIView alloc] initWithFrame:imageView.bounds]; imageView.contentMode = UIViewContentModeScaleAspectFit; self.navigationItem.titleView.autoresizesSubviews=NO; [self.navigationItem.titleView addSubview:imageView];` – Krunal Jul 18 '13 at 11:03
  • What is the size of the imageView? it should be the image.size – Lirik Jul 18 '13 at 11:12