自定义标签栏不显示导航栏
Posted
技术标签:
【中文标题】自定义标签栏不显示导航栏【英文标题】:Custom tab bar doesn't show navigation bar 【发布时间】:2017-03-27 08:04:18 【问题描述】:
我目前正在使用多故事板项目,并且我在 UIViewController 中创建了一个自定义UIView
(我将其命名为“Tab Bar VC”),并使用此链接中的教程使其看起来像一个标签栏:https://github.com/codepath/ios_guides/wiki/Creating-a-Custom-Tab-Bar
一切正常显示,但问题是当我按下标签栏项目时,导航栏不显示,没有它我无法推送到另一个视图控制器。我试图像这张图片一样将我的“Tab Bar VC”嵌入到导航控制器中,但它不起作用:
请注意,就我而言,我不想使用UITabBarViewController
或Storyboard Reference
。请帮帮我。
预先感谢。
这是我的 Tab Bar VC 代码:
#import "SHTabViewController.h"
#import "SHHomeViewController.h"
#import "SHTicketViewController.h"
#import "SHNotificationViewController.h"
#import "SHChatViewController.h"
#import "SHCallViewController.h"
@interface SHTabViewController ()
NSMutableArray *viewcontrollers;
NSInteger selectedIndex;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *tabButtons;
@property (strong, nonatomic) SHHomeViewController *homeVC;
@property (strong, nonatomic) SHCallViewController *callVC;
@property (strong, nonatomic) SHChatViewController *chatVC;
@property (strong, nonatomic) SHTicketViewController *ticketVC;
@property (strong, nonatomic) SHNotificationViewController *notifVC;
@end
@implementation SHTabViewController
-(void)viewWillAppear:(BOOL)animated
[super viewWillAppear:animated];
selectedIndex = 0;
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view.
selectedIndex = 0;
UIStoryboard *homeStoryboard = [UIStoryboard storyboardWithName:@"Home" bundle:[NSBundle mainBundle]];
UIStoryboard *callStoryboard = [UIStoryboard storyboardWithName:@"CallPM" bundle:[NSBundle mainBundle]];
UIStoryboard *chatStoryboard = [UIStoryboard storyboardWithName:@"Chat" bundle:[NSBundle mainBundle]];
UIStoryboard *ticketStoryboard = [UIStoryboard storyboardWithName:@"Ticket" bundle:[NSBundle mainBundle]];
UIStoryboard *notifStoryboard = [UIStoryboard storyboardWithName:@"Notification" bundle:[NSBundle mainBundle]];
self.homeVC = [homeStoryboard instantiateViewControllerWithIdentifier:@"homeVC"];
self.callVC = [callStoryboard instantiateViewControllerWithIdentifier:@"callVC"];
self.chatVC = [chatStoryboard instantiateViewControllerWithIdentifier:@"chatVC"];
self.ticketVC = [ticketStoryboard instantiateViewControllerWithIdentifier:@"ticketVC"];
self.notifVC = [notifStoryboard instantiateViewControllerWithIdentifier:@"notificationVC"];
viewcontrollers = [NSMutableArray new];
[viewcontrollers addObject:self.homeVC];
[viewcontrollers addObject:self.callVC];
[viewcontrollers addObject:self.chatVC];
[viewcontrollers addObject:self.ticketVC];
[viewcontrollers addObject:self.notifVC];
[self.tabButtons[selectedIndex] setSelected:YES];
[self didPressTab:self.tabButtons[selectedIndex]];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
- (IBAction)didPressTab:(UIButton *)sender
selectedIndex = 0;
sender.selected = YES;
NSInteger previousIndex = selectedIndex;
selectedIndex = sender.tag;
[self.tabButtons[previousIndex] setSelected:NO];
UIViewController *previousVC = viewcontrollers[previousIndex];
[previousVC willMoveToParentViewController:nil];
[previousVC.view removeFromSuperview];
[previousVC removeFromParentViewController];
UIViewController *vc = viewcontrollers[selectedIndex];
[self addChildViewController:vc];
vc.view.frame = self.contentView.bounds;
[self.contentView addSubview:vc.view];
[vc didMoveToParentViewController:self];
【问题讨论】:
【参考方案1】:不要在 UINavigationViewController 中嵌入“Tab bar VC”。将 UINavigationViewController 与您在“标签栏 VC”中添加的每个单独的 UIViewController 一起嵌入。
【讨论】:
我试过了,但是不行。而且我没有使用 TabBarController,我使用自定义视图并使其看起来像一个标签栏。【参考方案2】:您应该使用导航控制器嵌入所有单独的视图控制器。
TabBarController > NavigationController > 视图控制器
【讨论】:
我试过了,但是不行。而且我没有使用 TabBarController,我使用自定义视图并使其看起来像一个标签栏。【参考方案3】: For this you need to add a NavigationController for every Controllers like below
-(void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view.
selectedIndex = 0;
UIStoryboard *homeStoryboard = [UIStoryboard storyboardWithName:@"Home" bundle:[NSBundle mainBundle]];
UIStoryboard *callStoryboard = [UIStoryboard storyboardWithName:@"CallPM" bundle:[NSBundle mainBundle]];
UIStoryboard *chatStoryboard = [UIStoryboard storyboardWithName:@"Chat" bundle:[NSBundle mainBundle]];
UIStoryboard *ticketStoryboard = [UIStoryboard storyboardWithName:@"Ticket" bundle:[NSBundle mainBundle]];
UIStoryboard *notifStoryboard = [UIStoryboard storyboardWithName:@"Notification" bundle:[NSBundle mainBundle]];
self.homeVC = [homeStoryboard instantiateViewControllerWithIdentifier:@"homeVC"];
self.callVC = [callStoryboard instantiateViewControllerWithIdentifier:@"callVC"];
self.chatVC = [chatStoryboard instantiateViewControllerWithIdentifier:@"chatVC"];
self.ticketVC = [ticketStoryboard instantiateViewControllerWithIdentifier:@"ticketVC"];
self.notifVC = [notifStoryboard instantiateViewControllerWithIdentifier:@"notificationVC"];
UINavigationController *homeNavController = [[UINavigationController alloc] initWithRootViewController: self.homeVC];
UINavigationController *callVCNavController = [[UINavigationController alloc] initWithRootViewController: self.callVC];
UINavigationController *chatVCNavController = [[UINavigationController alloc] initWithRootViewController: self.chatVC];
UINavigationController *ticketVCNavController = [[UINavigationController alloc] initWithRootViewController: self.ticketVC];
UINavigationController *notifVCNavController = [[UINavigationController alloc] initWithRootViewController: self.notifVC];
viewcontrollers = [NSMutableArray new];
[viewcontrollers addObject: homeNavController];
[viewcontrollers addObject: callVCNavController];
[viewcontrollers addObject:ticketVCNavController];
[viewcontrollers addObject:chatVCNavController];
[viewcontrollers addObject: notifVCNavController];
[self.tabButtons[selectedIndex] setSelected:YES];
[self didPressTab:self.tabButtons[selectedIndex]];
【讨论】:
我试过了。它崩溃并返回错误:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported'
哦..亲爱的朋友,我遇到了您的问题,您已经采用了自定义 UIView,您在其中添加了一些按钮,例如 Tabbar,并且在该按钮动作加载控制器上作为子项。这就是您无法显示导航栏的原因
做一件事添加这个控制器父在所有控制器和父类中为每个控制器编写导航逻辑..父控制器UI显示在所有上述基本控制器上看起来像Tabbar..
我的意思是。抱歉,因为演示图片看起来像一个真正的标签栏。所以你对此感到困惑。你能帮我解决这个问题吗?我在每个屏幕上都缺少导航栏,我可以推送到其他 vc
我的第二条评论是您的问题解决方案...如下所示【参考方案4】:
您使用了错误的方法。您将标签栏控制器嵌入到导航控制器中,这在逻辑上没有任何意义。相反,您应该将各种导航控制器嵌入到一个标签栏控制器中。尝试做这样的事情:
【讨论】:
以上是关于自定义标签栏不显示导航栏的主要内容,如果未能解决你的问题,请参考以下文章
ecshop按自定义导航栏上面的标签最下面的一排文章标签不居中 偏左