ios - UINavigationBar 标题 View 对齐问题

标签 ios iphone objective-c ios7 uinavigationbar

我已将 ImageView 设置为特定 ViewController 的标题 View ,而其他 Controller 将 UILabel 设置为标题 View 。我使用以下代码设置标题 View ,

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

问题是,当我将标题 View 弹出回 View Controller 时 在左边停留几秒钟。我不确定这是什么原因?请让我知道哪里出错了???! enter image description here

P.S: 此问题仅在 iOS 7 中出现!

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

更新

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

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

最佳答案

正确设置导航有两种选择。

首先:

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

输出如下:-

enter image description here

第二:

将导航图像设置为您对特定 view-controller 的要求。使用相同大小的导航栏创建导航图像。并使用波纹管代码进行设置。

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

关于ios - UINavigationBar 标题 View 对齐问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23006033/

相关文章:

c# - 如何更改 Xamarin.forms 中 Alertbox 的大小

ios - 这里映射偏移位置指示器中心

iphone - CoreGraphics 替代品?

ios - 如何设置和获取指针 Swift 的值

ios - 有什么方法可以控制何时使用 Game Center 进行身份验证?

iphone - UITableView 无限滚动

ios - 即使在 iphone 设备上安装根证书后,Charles 代理也不起作用

ios - 日期格式很奇怪

ios - UISearchController searchBar 框架仅在第一次点击时调整大小

objective-c - 为 Mac 应用创建帮助手册