【问题标题】:Set a different background image for UINavigationBar in iOS 5在 iOS 5 中为 UINavigationBar 设置不同的背景图片
【发布时间】:2012-04-11 04:08:00
【问题描述】:

我的应用有多个 viewController,对于其中一些,我想在将它们推送到导航堆栈时使用不同的导航栏背景图像。

例如,当我的 mainViewController 加载时,我有这个:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"Setting toolbar for iOS 5+");
    UIImage * navbarImage = [UIImage imageNamed:@"navbar01.png"];
    [[UINavigationBar appearance] setBackgroundImage:navbarImage forBarMetrics:UIBarMetricsDefault];
}

同样,当我的另一个 viewController 被推送到导航堆栈时,我使用与上面相同的代码,除了使用不同的 UIImage (navbar02.png)。

但是,导航栏与我设置的第一张图片相比并没有改变。有什么办法可以在出现新视图时更改 UINavigationBar 背景图片?

谢谢!

【问题讨论】:

    标签: iphone uinavigationbar background-image


    【解决方案1】:

    这里是类似问题的链接:UINavigationBar setBackgroundImage: forBarMetrics: Not Working

    答案是用这个:

    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    

    而不是你使用的代码。

    【讨论】:

      【解决方案2】:
      float version = [[[UIDevice currentDevice] systemVersion] floatValue];
      //  NSLog(@"%f",version);
      if (version >= 5.0) {
          UIImage *backgroundImage = [UIImage imageNamed:@"image.png"];
         [self.navigationController.navigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
      }
      else
      {
         // UIImage *backgroundImage = [UIImage imageNamed:@"image.png"];
          NSString *barBgPath = [[NSBundle mainBundle] pathForResource:@"NavBar" ofType:@"png"];
          [self.navigationController.navigationBar.layer setContents:(id)[UIImage imageWithContentsOfFile: barBgPath].CGImage];
      }
      

      它对我来说很成功......也许它会帮助你。

      【讨论】:

        猜你喜欢
        • 2017-09-22
        • 2012-03-07
        • 1970-01-01
        • 1970-01-01
        • 2014-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多