1

I have set an ImageView as title view for a specific ViewController whereas the other controllers have UILabel as titleView. I used the following code to set title view,

UIImageView *imageTitle = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Logo"]];
[self.navigationController.navigationBar.topItem setTitleView:imageTitle];

I have tried using this one as well,

UIImageView *imageTitle = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Logo"]];
[self.navigationItem setTitleView:imageTitle];

The issue is, when I pop back to the view controller the title view stays on left for few seconds. I'm not sure what's the reason for this? Please let me know where I'm going wrong???! enter image description here

P.S: The issue occurs in iOS 7 only!

Here is the sample project with this issue - https://dl.dropboxusercontent.com/u/97646145/Issue/NavigationTitleView.zip

Update

As @FahimParkar suggested, I used a 320x44 image for the titleview. The delay in positioning seems little as the image is wide. Is this the only solution for this issue?

Link to download trial project with 320x44 image -> https://dl.dropboxusercontent.com/u/97646145/Issue/NavigationTitleView_Updated.zip

Nina
  • 1,579
  • 2
  • 21
  • 51

4 Answers4

1

You have two choise for setting navigation properly.

First:

setting Titleview as you did like bellow:-

- (void)viewDidLoad
{

    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
    UIImageView *imgviw=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    imgviw.backgroundColor = [UIColor blackColor];

    imgviw.center=navView.center; // this is display your image at center of navigation bar.
    imgviw.image=[UIImage imageNamed:@"passowrd.png"];
    [navView addSubview:imgviw];

    self.navigationItem.titleView = navView;
    [super viewDidLoad];
}

That Output like:-

enter image description here

Second:

Setting navigation image as par your requirement for particulare view-controller. create image for navigation with same size of navigation bar. And set using Bellow code.

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Navbar.png"] forBarMetrics:UIBarMetricsDefault];
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
0

May be there is a issue in autoresizing

UIImageView* imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"LOGO.png"]];
imgView.autoresizingMask=UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
imgView.contentMode=UIViewContentModeScaleAspectFit;
self.navigationItem.titleView = imgView;
Himanshu Joshi
  • 3,391
  • 1
  • 19
  • 32
0

Try this code in your ViewWillAppear method hope this helps you:

UIImage *image = [UIImage imageNamed: @"Logo"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];

self.navigationItem.titleView = imageView;
Rajat
  • 1,043
  • 12
  • 23
0

You need to setup your navigationbar title view inside viewDidLoad and not viewWillAppear.

fsegouin
  • 1
  • 1