Leo Jweda
Leo Jweda

Reputation: 2487

How can I embed Split View controller in Tab Bar controller?

I started with a Master-Detail Application, then went to the iPhone storyboard, selected the NavigationController and went to Editor > Embed In > TabBarController and it worked just fine. When I tried to do the same in the iPad storyboard the Embed in TabBarController menu item was disabled. Should I just do that manually?

Upvotes: 1

Views: 4659

Answers (4)

user2875404
user2875404

Reputation: 3218

Based on Rob George's Swift answer, here is the Objective-C code:

UITabBarController *tabBarViewController= (UITabBarController *) self.window.rootViewController;
NSLog(@"%lu",tabBarViewController.viewControllers.count);
UISplitViewController *splitViewController = (UISplitViewController *)nil;

for(UIViewController *viewController in tabBarViewController.viewControllers){
    if([viewController.title isEqualToString:@"Master"]){
        splitViewController= (UISplitViewController *) viewController;
    }
}

UINavigationController *navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1];
navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem;
splitViewController.delegate = self;

Took me a while because "first" ViewController was white and with the bar (with no icons and white text) being almost perfectly white, too, I thought the app just defaults to displaying nothing but actually it was picking the ViewController that was empty and I simply didn't see the bar. Just in case you were wondering why it doesn't work lol

Upvotes: 1

Rob George
Rob George

Reputation: 126

  1. Create a sample Master/Detail via xcode
  2. Drag a tabViewController on the storyboard
  3. Change the TabViewController to the initial view controller.
  4. Control Drag from the TabViewController to the SplitViewControler
  5. Assign a title to the SplitViewControler in the storyboard (I Used "Master"
  6. In the app delegate replace the reference to the splitViewController as follows.

    let tabBarViewController = self.window!.rootViewController as! UITabBarController
    print(tabBarViewController.viewControllers?.count)
    var splitViewController:UISplitViewController? = nil
    for viewController in tabBarViewController.viewControllers! {
        if viewController.title == "Master" {
            splitViewController = viewController as? UISplitViewController
        }
    }
    
    let navigationController = splitViewController!.viewControllers[splitViewController!.viewControllers.count-1] as! UINavigationController
    navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController!.displayModeButtonItem()
    splitViewController!.delegate = self
    

Upvotes: 7

Jay Versluis
Jay Versluis

Reputation: 2072

The Split View Controller can't be embedded into anything - it needs to be the root view controller in your storyboard.

You can however select either the Master View Controller or the Detail View Controller and embed those classes into other view controllers.

Upvotes: 0

vignesh kumar
vignesh kumar

Reputation: 2330

In my application I embed programmatically like below

        UISplitViewController *split=[[UISplitViewController alloc]init];
        PatientListController *patListContr=[[PatientListController alloc]init];
        PatientController *patientControl=[[PatientController alloc]init];
        split.viewControllers=@[patListContr,patientControl];
        split.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"" image:[UIImage imageNamed:@"patient.png"] selectedImage:nil]; 

Then embed the above split view controller to a TabbarController

UITabBarController *tabbarContr=[[UITabBarController alloc]init];
        tabbarContr.viewControllers=@[split,yourController1,yourController2];

Upvotes: 1

Related Questions