ios tabBarControl、navigationcontroller 标题和按钮编辑
Posted
技术标签:
【中文标题】ios tabBarControl、navigationcontroller 标题和按钮编辑【英文标题】:ios tabBarControl, navigationcontroller title and button editing 【发布时间】:2014-05-06 18:41:48 【问题描述】:我一直在我的应用程序中使用标准的 TabBarController 和顶部 NavigationBar。 tabBar 在 appDelegate 中加载,初始化 navigationBar 以使用图像而不是中心标题,这适用于所有四个 tabBar 视图。偶尔我会将一个新视图推送到视图控制器上,效果很好。
我想做的是能够在打开 4 个 tabBar 视图控制器时更改它们中的每一个的 NavigationBar。我很难做到这一点。我的初始导航栏有一个图像,加载在 appDelegate 中。我的问题是:
每个 viewController 通常是否应该根据需要在各自的 viewWillAppear 方法中从头开始创建一个新的 navigationBar? 应该编辑哪些navigationController 和navigationBar——总是appDelegate 的navigationController、tabBarController.navigationController,或者只是每个视图中的self.navigationController? (通常,大多数编辑对我不起作用,不会发生任何更改) 如果我用 imageView 覆盖标准标题(通常是 tabBar 的当前视图的标题),并希望另一个 tabBar 视图使用标准标题,我应该如何删除 titleView 图像,并重置回文本?反之亦然,取决于视图?这就是我真正想要实现的目标,但没有成功。我想我正在寻找管理每个视图的导航栏的标准做法,并为每个 tabBarItem 更改它。
// in appDelegate
- (void)initNav
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate = self;
self.tabBarController.navigationController.view.backgroundColor = [UIColor whiteColor];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:self.firstViewController,
self.secondViewController,
self.thirdViewController,
self.forthViewController,
nil];
[self.window addSubview:self.tabBarController.view];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.tabBarController];
self.navigationController.navigationBarHidden = NO;
// customize background color of nav bar to "blue"ish color
self.navigationController.navigationBar.backgroundColor = [UIColor colorWithHexString:@"00A3E1"];
self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:@"00A3E1"];
self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"00A3E1"];
[self createNavs];
// also in appDelegate
- (void)createNavs
// white text when present
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],
UITextAttributeTextColor,
[UIColor clearColor],
UITextAttributeTextShadowColor, nil];
[[UIBarButtonItem appearance] setTitleTextAttributes: attributes
forState: UIControlStateNormal];
AppDelegate *delegateRef = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[self.navigationController.navigationBar setTranslucent:NO];
// setup left button (currently unused -- no left button)
/*
/*
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"glyphicons_049_star.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(starClick:) forControlEvents:UIControlEventTouchDown];
[button setFrame:CGRectMake(0, 0, 25, 25)];
self.navigationController.navigationBar.topItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
*/
// setup logo in center
self.tabBarController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"synced_top_logo.png"]];
// setup right button (currently unused -- no right button)
/*
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] init];
rightButton.title = @"edit";
[rightButton setTarget:self ];
[rightButton setAction:@selector(editClick:)];
self.navigationController.navigationBar.topItem.rightBarButtonItem = rightButton;
*/
[[self navigationController] setNavigationBarHidden:NO animated:YES];
[[delegateRef navigationController] setNavigationBarHidden:NO animated:YES];
编辑: 这是下面发布的解决方案的简化版本,它很有帮助,并允许进行所需的自定义。每个视图控制器都独立于此自定义其导航栏。大概从一开始就应该这样做。
tabBarController.viewControllers = [NSArray arrayWithObjects:[[UINavigationController alloc] initWithRootViewController:self.firstViewController],
[[UINavigationController alloc] initWithRootViewController:self.secondViewController],
[[UINavigationController alloc] initWithRootViewController:self.thirdViewController],
[[UINavigationController alloc] initWithRootViewController:self.forthViewController],
nil];
【问题讨论】:
所以让我明白这一点。你有一个UITabBarController
包裹在 UINavigationController
中?因此,在显示UITabBarController
的第一个视图中,当您推送到新的UIViewController
时,您希望UITabBarController
选项卡栏消失吗?我问是因为这不是使用UITabBarController
时的惯例。您不应将 UITabBarController
包装在 UINavigationController
中。
@Miro,只是一个疯狂的猜测,但你是否尝试过阅读
@random,这似乎是正在发生的事情。我相信我最初这样做是因为导航栏最初将是 100% 静态的。现在它已经达到了极限。什么是最好的方法,每个 ViewController 添加到 TabBarController 的 NavigationController?他们现在需要独特的标题 + 按钮,有些人会推送其他视图。
【参考方案1】:
这可能是你需要的:
- (void)initTabbar
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate = self;
/*
You want each of your UIViewControllers to be wrapped in a UINavigationController. Then put each of those UINavigationControllers in a UITabBarController
*/
//You don't need to hang on to this becuase the proceeding UINavigationController will handle it
FirstViewController *firstViewController = [[FirstViewController alloc] ...];
//You'll need to declare this in your header
self.firstNavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
//Second view allocation
SecondViewController *secondViewController = [[SecondViewController alloc] ...];
self.secondNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
//Third view allocation
ThirdViewController *thirdViewController = [[ThirdViewController alloc] ...];
self.thirdNavigationController = [[UINavigationController alloc] initWithRootViewController:thirdViewController];
//Now you add each of the UINavigationControllers (which is a subclass of UIViewController) to the UITabBarController.
self.tabBarController.viewControllers = [NSArray arrayWithObjects:self.firstNavigationController,
self.secondNavigationController,
self.thirdNavigationController,
nil];
[self.window addSubview:self.tabBarController.view];
[self createNavs];
//This is more of a 'formatNavs' now
- (void)createNavs
//Now you can customize each of the UINavigationController's UINavigationBars seperatly
self.firstNavigationController.navigationBar.backgroundColor = [UIColor colorWithHexString:@"00A3E1"];
self.firstNavigationController.navigationBar.tintColor = [UIColor colorWithHexString:@"00A3E1"];
self.firstNavigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"00A3E1"];
self.secondNavigationController.navigationBar.backgroundColor = [UIColor colorWithHexString:@"...."];
self.secondNavigationController.navigationBar.tintColor = [UIColor colorWithHexString:@"...."];
self.secondNavigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"...."];
self.thirdNavigationController.navigationBar.backgroundColor = [UIColor colorWithHexString:@"...."];
self.thirdNavigationController.navigationBar.tintColor = [UIColor colorWithHexString:@"...."];
self.thirdNavigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"...."];
【讨论】:
非常好。我在上面添加了一个编辑,它简化了相同的逻辑,但每个选项卡都有自己的 navigationController 堆栈。以上是关于ios tabBarControl、navigationcontroller 标题和按钮编辑的主要内容,如果未能解决你的问题,请参考以下文章
iOS 自定义TabBarController zhuanzai