【问题标题】:UINavigationBar title view alignment issueUINavigationBar 标题视图对齐问题
【发布时间】:2014-04-11 07:21:24
【问题描述】:

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

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

问题是,当我回到视图控制器时,标题视图 在左侧停留几秒钟。我不确定这是什么原因?请让我知道我哪里出错了???

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

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

更新

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

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

【问题讨论】:

  • @DhavalBhadania 没有。一个是关于导航栏的……另一个是关于如何设置标题视图的。我已经设置了标题视图,但是弹出时出现问题!
  • 几秒钟??然后就没事了?
  • [self.navigationItem setTitleView:imageTitle]; 这是正确的实现
  • @HimanshuJoshi 是的.. 它只发生几秒钟.. 然后就可以了

标签: ios iphone objective-c ios7 uinavigationbar


【解决方案1】:

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

第一:

像下面这样设置 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 的要求。为导航栏创建相同大小的导航图像。并使用 Bellow 代码设置。

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

【讨论】:

    【解决方案2】:

    在您的 ViewWillAppear 方法中尝试此代码,希望对您有所帮助:

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

    【讨论】:

      【解决方案3】:

      autoresizing可能有问题

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

      【讨论】:

      • 在 titleView 中添加图片的位置?在viewDidLoadviewWiilAppear?
      • 我有一个单独的类来堆叠所有主视图控制器,我在那个类中做这部分..
      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-05
        • 1970-01-01
        • 1970-01-01
        • 2012-09-29
        • 1970-01-01
        相关资源
        最近更新 更多