Sometimes you may want your splitviewController to be displayed within a TabBarController of an ipad application.
However we can go for either “Tab Based” application or “Split View Based” application but not a combination of both. But many IOS applications are tab based and it would be good if the existing tab based apps can be migrated to have ipad specific split views inside the tabs.
From Apple’s documentation this is a no no situation. But there is a way to achieve this easily (we did it for an application) and the following are the steps to achieve it.
On running sample app expected output will be similar to images below
However we can go for either “Tab Based” application or “Split View Based” application but not a combination of both. But many IOS applications are tab based and it would be good if the existing tab based apps can be migrated to have ipad specific split views inside the tabs.
From Apple’s documentation this is a no no situation. But there is a way to achieve this easily (we did it for an application) and the following are the steps to achieve it.
- Create your own mySplitController extending UIViewController.
- Have an ivar and retainableProperty named leftController.
- Have an ivar and retainableProperty named rightController.
- Have a method as below method as below
-(mySplitController*) initwithLeftVC:(UIViewController*)leftvc rightVC:(UIViewController*)rightvc { if(self=[super init]) { UINavigationController *lnc=[[UINavigationController alloc] initWithRootViewController:leftvc]; lnc.navigationBarHidden=NO; self.leftController=lnc; [lnc release]; UINavigationController *rnc=[[UINavigationController alloc] initWithRootViewController:rightvc]; rnc.navigationBarHidden=NO; self.rightController=rnc; [rnc release]; } return self; }
- Add a assigned property named detailNavigationController of type UINavigationController to your Root(/Left) VC.
@interface RootVC : UIViewController
{ UINavigationController *detailedNavigationController; } @property(nonatomic,assign) UINavigationController *detailedNavigationController; @end @implementation RootVC @synthesize detailedNavigationController; @end
- Now if RootVC is your root controller and DetailedVC is right/detail controller then the following code initialize it for you.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. UITabBarController *tabBarController = [[UITabBarController alloc] init]; //set tbconroller as root view controller of window [self.window setRootViewController:tabBarController]; //window retains controller so we can release [tabBarController release]; //create split view controller RootVC *rvc=[[RootVC alloc] init]; DetailedVc *dvc=[[DetailedVc alloc] init]; mySplitController *msc = [[mySplitController alloc] initwithLeftVC:rvc rightVC:dvc]; //give access to detailed naviigation for rootvc rvc.detailedNavigationController=msc.rightController; //create a temporary VC to show in second tab UIViewController *vc2 = [[UIViewController alloc] init]; //make an array containing these two view controllers NSArray *viewControllers = [NSArray arrayWithObjects:msc,vc2,nil]; [tabBarController setViewControllers:viewControllers]; //the views are retained their new owners, so we can release [msc release]; [vc2 release]; [self.window addSubview:tabBarController.view]; self.window.rootViewController=tabBarController; [[self window] makeKeyAndVisible]; return YES; }
On running sample app expected output will be similar to images below
- Tab with a split view in it
- Second Tab with a normal view in it