How to put an image in the center of navigationBar of a UIViewController? How to put an image in the center of navigationBar of a UIViewController? ios ios

How to put an image in the center of navigationBar of a UIViewController?


The UINavigationController manages the navigation bar by looking at the navigationItem property of the top-most view controller on the navigation stack. So to change the view to a logo, you need to set this up in the view controller that uses the logo (i.e. the root view controller or another one that gets pushed on the stack).

Do something like this in viewDidLoad of your view controller:

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

In your case, you are setting the wrong navigation item:

// Oops...self.navigationController.navigationItem.titleView = logoView;// Should be this:self.navigationItem.titleView = logoView;


First we have to create a view which have size as same as navigation bar then add an image view and set set its frame as it looks center in the navigation bar.It works for all ios version and it automatically takes frame size as per device (retina or normal) and works like magic.

UIView *headerView = [[UIView alloc] init];headerView.frame = CGRectMake(0, 0, 320, 44);UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Header.png"]];imgView.frame = CGRectMake(75, 0, 150, 44);imgView.contentMode = UIViewContentModeScaleAspectFit;[headerView addSubview:imgView];navigationCtrl.navigationBar.topItem.titleView = headerView;[headerView release];[imgView release];


Swift:

var logoImage:UIImage = UIImage(named: "logo_text")!self.navigationItem.titleView = UIImageView(image: logoImage)