简体   繁体   English

UINavigationBar标题视图对齐问题

[英]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. 我已经将ImageView设置为特定ViewController的标题视图,而其他控制器将UILabel为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! PS:该问题仅发生在iOS 7中!

Here is the sample project with this issue - https://dl.dropboxusercontent.com/u/97646145/Issue/NavigationTitleView.zip 这是存在此问题的示例项目-https: //dl.dropboxusercontent.com/u/97646145/Issue/NavigationTitleView.zip

Update 更新

As @FahimParkar suggested, I used a 320x44 image for the titleview. 正如@FahimParkar所建议的那样,我使用了320x44的图片作为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 链接下载具有320x44图像的试用项目-> https://dl.dropboxusercontent.com/u/97646145/Issue/NavigationTitleView_Updated.zip

Try this code in your ViewWillAppear method hope this helps you: 在您的ViewWillAppear方法中尝试以下代码,希望对您ViewWillAppear帮助:

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:- 像下面一样设置Titleview:-

- (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 . 将导航图像设置为特定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 可能存在的问题autoresizing

UIImageView* imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"LOGO.png"]];
imgView.autoresizingMask=UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
imgView.contentMode=UIViewContentModeScaleAspectFit;
self.navigationItem.titleView = imgView;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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