自定义标签栏不显示导航栏

时间:2017-03-27 08:04:18

标签: ios objective-c uiviewcontroller uinavigationcontroller uitabbar


我目前正在使用多故事板项目,我在UIViewController中创建了一个自定义UIView(我将其命名为“Tab Bar VC”)并使其看起来像一个标签栏,其中包含此链接中的教程:
https://github.com/codepath/ios_guides/wiki/Creating-a-Custom-Tab-Bar
一切都正常显示,但问题是当我按下导航栏没有显示的标签栏项目时,如果没有它,我就无法推送到另一个视图控制器。我试图在导航控制器中嵌入我的“Tab Bar VC”,就像这张照片一样,但它不起作用:
请注意,在我的情况下,我不想使用UITabBarViewControllerStoryboard Reference。请帮帮我 预先感谢。 enter image description here 这是我的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];
}

4 个答案:

答案 0 :(得分:0)

不要嵌入标签栏VC'使用UINavigationViewController。将UINavigationViewController与您在“标签栏VC”中添加的每个UIViewControllers一起嵌入。

答案 1 :(得分:0)

您应该使用导航控制器嵌入所有单独的视图控制器。

TabBarController> NavigationController>的viewController

答案 2 :(得分:0)

    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]];

答案 3 :(得分:0)

你使用的是错误的做法。您将标签栏控制器嵌入到导航控制器中,逻辑上没有任何意义。相反,您应该将各种导航控制器嵌入到一个标签栏控制器中。尝试做这样的事情:enter image description here