简体   繁体   中英

UINavigationBar title view alignment issue

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??? ! 在此处输入图片说明

PS: 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

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;

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:-

在此处输入图片说明

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];

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;

您需要在viewDidLoad而不是viewWillAppear设置导航viewDidLoad标题视图。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM