【问题标题】:How can I change the UINavigationBar of a UINavigationController?如何更改 UINavigationController 的 UINavigationBar?
【发布时间】:2011-10-23 22:31:41
【问题描述】:

我有一个正在以编程方式创建的 UINavigationController:

 MessageTableViewController* homeStream = [[MessageTableViewController alloc] initWithNibName:@"MessageTableViewController" bundle:nil];
          homeStream.fetchHomeStream = YES;
          homeStream.title = @"Home";
          UINavigationController* navBarController = [[UINavigationController alloc] initWithRootViewController:homeStream];
          navBarController.tabBarItem.image = [UIImage imageNamed:@"hometab.png"];
          [homeStream release];

我有一个名为 STNavigationBar 的自定义 UINavigationBar。如何使用 STNavigationBar 代替 UINavigationController 中的默认 UINavigationBar?

【问题讨论】:

    标签: iphone objective-c cocoa-touch uinavigationcontroller uinavigationbar


    【解决方案1】:

    我最终使用了 iOS5 UIKit 外观代理:

    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"header"] forBarMetrics:UIBarMetricsDefault];
    

    不需要子类化或类别。

    【讨论】:

      【解决方案2】:

      编辑供未来的读者参考,下面的答案在 iOS 5 上不再适用。

      您不能只将其设置为现有实例,因为 UINavigationController 具有 navigationBarreadonly 属性。而且我不认为继承 UINavigationController 是你想要的。

      我认为最好的方法是创建一个扩展 UINavigationBar 的类别,如 here 所述。

      【讨论】:

      • 我最初将它作为一个运行良好的类别,但 iOS5 破坏了此功能,现在使用类别覆盖实际方法是一种不好的做法。所以我决定对它进行子类化。当我在 IB 中设置导航栏时它可以工作,但我还需要以编程方式设置它...
      • 我没有意识到这一点。选项号呢? 3 在这个答案? stackoverflow.com/questions/1869331/…
      • 刚刚发了一个答案,我用的是iOS5 UIKit外观代理
      猜你喜欢
      • 2017-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多