Reputation: 6490
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.
Upvotes: 8
Views: 11711
Reputation: 3187
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];
Upvotes: 1
Reputation: 2100
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.
Upvotes: 22
Reputation: 20541
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];
}
Upvotes: 3