如何为所有导航控制器和视图控制器设置自定义导航栏(title + titleView)? [英] How do I set custom navigation bar(title + titleView) for all navigation controllers and view controller?

查看:57
本文介绍了如何为所有导航控制器和视图控制器设置自定义导航栏(title + titleView)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选项卡式应用程序,在选项卡中有导航控制器,在其中有视图控制器.它们全部使用相同的导航控制器导航栏:后退"按钮+徽标图像.目前,我将此代码放置在每个视图控制器中:

I have a tabbed application with navigation controllers in tabs and view controller in them. All of them use the same navigation controller navigation bar: back button + logo image. Currently, I'm placing this code in every view controller:

- (void)viewDidLoad
{
  [super viewDidLoad];
  self.navigationItem.title = @"Back";
  UIImage *headerImage = [UIImage imageNamed:@"Logo.png"];
  self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:headerImage] autorelease];
}

恕我直言,这不是最好的方法.我正在考虑为UIViewController使用类别并重写viewDidLoad方法,但是每个视图控制器都有一些其他代码要在viewDidLoad中执行,因此我想重写不是解决方案.还有什么其他方法?

IMHO it's not the best way to do that. I'm thinking about using a category for UIViewController and to override viewDidLoad method, but every view controller has some additional code to execute in viewDidLoad, so I guess overriding is not the solution. What are the other ways?

推荐答案

您可以创建 UIViewController MyViewControllerWithBackButtonAndLogo 的子类(可以提供更好的名称)然后实施 viewDidLoad 设置导航项.使所有视图控制器都继承自该基类.然后只需确保视图控制器在其自己的实现中调用 [super viewDidLoad] .

You could create a subclass of UIViewController, MyViewControllerWithBackButtonAndLogo (you can come up with a better name) then implement viewDidLoad to set up the navigation item. Make all your view controllers inherit from this base class. Then just make sure the view controllers call [super viewDidLoad] in their own implementations.

还要注意,要设置后退按钮标题,您不应更改导航项的 title 属性,而应设置适当的 backBarButtonItem .

Also note that to set the back button title, you should not change the navigation item's title property, but set an appropriate backBarButtonItem.

这篇关于如何为所有导航控制器和视图控制器设置自定义导航栏(title + titleView)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆