根据select哪个标签更改导航栏的标题?
我有一个iOS应用程序,其中有一个导航控制器作为根控制器,但在一个部分有一个标签栏来select导航栏之一的视图之间。 它看起来类似于iTunes应用程序(顶部的导航栏,底部的标签栏)。 我希望我的导航栏的标题根据select哪个标签进行更改。 我有两个单独的控制器文件为每个选项卡。 这是我曾尝试使用的每一个到目前为止解决这个无济于事:
self.navigationItem.title = @"Title"; self.navigationController.navigationItem.title = @"title"; [self.navigationController setTitle:@"Live"]; [self setTitle:@"Top Title"];
如何根据按下哪个选项卡来更改NavBar标题?
您更改当前正在显示的视图控制器中的栏的标题。
因此,例如,您在选项卡控制器中显示的视图控制器A中,可以添加:
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:YES]; [self setTitle:@"A"]; self.tabBarController.navigationItem.title = @"A"; }
B,C等也一样
在标签中的ViewController中:
-(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.tabBarController.title = self.title; }
如果标签栏控制器提供的单个视图控制器有自己的导航栏,那么
[self setTitle:@"Foo"];
将设置标签栏标签以及导航栏标题。
如果导航控制器位于顶层(即标签栏位于导航控制器内部),那么您可能需要手动设置导航栏标题(您将在viewDidAppear
而不是viewDidLoad
执行此操作,因为这些孩子控制器不会在每次切换时重新加载),例如:
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self.navigationController.navigationBar.topItem setTitle:@"Foo"]; }
或者,您可以在UITabBarControllerDelegate
方法didSelectViewController
执行此导航栏标题调整。
如果没有这样做,您可能需要澄清您的问题,描述控制器的层次结构(例如导航栏内的标签栏控制器,反之亦然)。
您可以inheritanceUITabBarController的子类,将委托设置为自己,并在select视图控制器时使用委托设置其标题:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { //change the title based on viewController that is selected self.title = @"New title"; }
只是两行代码..唯一的事情是,你需要使用viewWillAppear方法
-(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.tabBarController.navigationItem.title = @"Your Title"; }
PS:从以上答案启发…
UITabBarController *tabController = (UITabBarController *)self.parentViewController; tabController.navigationItem.title = @"ABC";
这是为我工作
从互联网上的一些研发
你必须通过导航项链。 UINavigationController显示属于它的topViewController的navigationItem,它是UITabBarController。 UITabBarController在其选项卡中显示navigationItem标题。 所以你需要做的是确保tabBarController的navigationItem是它的selectedViewController的navigationItem
所以回顾一下:
UINavigationController title = topViewController.navigationItem.title
UITabBarController tabTitle = selectedViewController.navigationItem.title
UIViewController title = navigationItem.title