繁体   English   中英

UINavigationBar标题视图对齐问题

[英]UINavigationBar title view alignment issue

我已经将ImageView设置为特定ViewController的标题视图,而其他控制器将UILabel为titleView。 我使用以下代码设置标题视图,

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

我也尝试过使用这个

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

问题是,当我弹出视图控制器时,标题视图会在左侧停留几秒钟。 我不确定这是什么原因? 请让我知道我要去哪里错了吗??? 在此处输入图片说明

PS:该问题仅发生在iOS 7中!

这是存在此问题的示例项目-https: //dl.dropboxusercontent.com/u/97646145/Issue/NavigationTitleView.zip

更新

正如@FahimParkar所建议的那样,我使用了320x44的图片作为titleview。 由于图像较宽,定位延迟似乎很小。 这是此问题的唯一解决方案吗?

链接下载具有320x44图像的试用项目-> https://dl.dropboxusercontent.com/u/97646145/Issue/NavigationTitleView_Updated.zip

在您的ViewWillAppear方法中尝试以下代码,希望对您ViewWillAppear帮助:

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

self.navigationItem.titleView = imageView;

您有两个选择可以正确设置导航。

第一:

像下面一样设置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];
}

该输出像:

在此处输入图片说明

第二:

将导航图像设置为特定view-controller要求。 创建具有与导航栏相同大小的导航图像。 并使用贝娄代码进行设置。

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Navbar.png"] forBarMetrics:UIBarMetricsDefault];

可能存在的问题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