How to add a image at the center of navigationBar in Objective-C?

Wun picture Wun · Jan 29, 2015 · Viewed 19.6k times · Source

I am developing in IOS , I use the following code to set a background to the navigationBar.

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bar-back"] forBarMetrics:UIBarMetricsDefault];

And I want to add a image on navigationBar and center of navigationBar like the following picture.

enter image description here

How to add a image at the center of navigationBar in Objective-C ?

Thanks in advance.

Answer

Jageen picture Jageen · Jan 29, 2015

Set navigation title view like below

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



EDIT:
if you have big image then use below code

UIImage *img = [UIImage imageNamed:@"1.png"];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[imgView setImage:img];
// setContent mode aspect fit
[imgView setContentMode:UIViewContentModeScaleAspectFit];
self.navigationItem.titleView = imgView;