add two navigation controller to one Tab bar Item

前端 未结 1 1154
温柔的废话
温柔的废话 2021-01-15 03:48

I want 2 navigation Controller to be attached to one Tab bar item.

Basically the idea is to have 2 views on a single tab Item and there should be a navigation bar to

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-15 04:34

    One way would be to create a UIViewController subclass that contains two navigation controllers and add that to the UITabBarController. Here's how you'd create and layout the navigation controllers in the UIViewController's -viewDidLoad method:

    FirstViewController *firstViewController = [[FirstViewController alloc] init];
    UINavigationController *firstNavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
    [firstViewController release];
    
    SecondViewController *secondViewController = [[SecondViewController alloc] init];
    UINavigationController *secondNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
    [secondViewController release];
    
    firstNavigationController.view.frame = CGRectMake(0.f, 0.f, 320.f, self.view.frame.size.height);
    firstNavigationController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    
    secondNavigationController.view.frame = CGRectMake(321.f, 0.f, self.view.frame.size.width - 321.f, self.view.frame.size.height);
    secondNavigationController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth |
                                                       UIViewAutoresizingFlexibleRightMargin;
    
    [self.view addSubview:firstNavigationController.view];
    [self.view addSubview:secondNavigationController.view];
    

    This is more or less how the UISplitViewController works under the hood.

    Edit: you might need to add the following code to make sure it lays out properly:

    - (void)viewWillAppear:(BOOL)animated { 
        [super viewWillAppear:animated];
        [firstNavigationController viewWillAppear:animated];
        [secondNavigationController viewWillAppear:animated];
    }
    
    - (void)viewWillDisappear:(BOOL)animated { 
        [super viewWillDisappear:animated];
        [firstNavigationController viewWillDisappear:animated];
        [secondNavigationController viewWillDisappear:animated];
    }
    
    - (void)viewDidAppear:(BOOL)animated { 
        [super viewDidAppear:animated];
        [firstNavigationController viewDidAppear:animated];
        [secondNavigationController viewDidAppear:animated];
    }
    
    - (void)viewDidDisappear:(BOOL)animated { 
        [super viewDidDisappear:animated];
        [firstNavigationController viewDidDisappear:animated];
        [secondNavigationController viewDidDisappear:animated];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return YES;
    }
    

    0 讨论(0)
提交回复
热议问题