简体   繁体   中英

UISplitViewController on universal app with Storyboard

I'm want to make an app that uses a UISplitViewControler on the iPad (as far as I understand it's only available on the iPad) but I want the app to be universal.

The setup is like this:

I have a UITableView (as a master view) and when I select a row it should display a detail view of that cell. I am using a Storyboard and I can't figure out how to implement the split view only for the iPad.

What would be the easiest way to achieve that? Thanks.

You don't need two storyboards to do this.You can use them both in a single storyboard.For iphone ,we normally use a class SWRevealViewController (if you are new to iOS coding ..:)) for side menu and splitviewcontroller for ipad.We can also use SWRevealViewController for ipad as well.It depends on your requirement.

For universal apps,create viewcontrollers using size Classes (usually we use any height any width for universal apps ).

change these size classes and create different viewcontrollers for ipad and iphones as required. In most cases any height any width will do the job.

After creating the viewcontrollers,in the appdelegate ,using the instantiateViewcontrollerWithIdentifier method, load the required viewcontroller.

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  // The device is an iPad running ios 3.2 or later.

}
else {
  // The device is an iPhone or iPod touch.
}

For ipad load the splitviewcontroller. and swrevealviewcontroller for iPhone.

This is the core basics.If you need any more information,let me know.

EDIT

Have you seen an arrowmark ath the initial VC(viewcontroller) in the storyboard?This vc is loaded first after the launch screen.In my app,I have a home screen which is common to both iphone and ipad(using size classes as mentioned above).So I can set this vc as the initial VC.In this case I don't have to do anything in the appdelegate.But if I have a different home screen for ipad,then I can make a condition check in the appdelegate didFinishLaunchingWithOptions

You can load the First screen like this.You should follow through splitVC tutorilal and swrevealcontroller tutorial to set the side menu.You should load the SWrevealVC or splitViewcontroller only if the first screen contains the side menu.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
    {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UISplitViewController *split = [storyboard instantiateViewControllerWithIdentifier:@"SplitViewController"];
        [AppDelegate setRootController:split storyboard:storyboard actiontype:0];
    }
    else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *split = [storyboard instantiateViewControllerWithIdentifier:@"SWrevealVC"];
        [AppDelegate setRootController:split storyboard:storyboard actiontype:-1];
    }
return YES;
}

+(void)setRootController:(UIViewController*)controller
              storyboard:(UIStoryboard*)storyboard actiontype:(int) actiontype;
{
    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && actiontype == 0)
    {
        UISplitViewController *splitViewController = (UISplitViewController *)controller;
        //splitViewController.presentsWithGesture = false;

        UINavigationController *masterNavigationController = [splitViewController.viewControllers objectAtIndex:0];
        SideMenuViewController *controller = (SideMenuViewController *)masterNavigationController.topViewController;
        controller.splitViewController = splitViewController;
        splitViewController.delegate = (id)controller;
    }

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    [UIView
     transitionWithView:appDelegate.window
     duration:0.5
     options:UIViewAnimationOptionAllowAnimatedContent
     animations:^(void) {
         BOOL oldState = [UIView areAnimationsEnabled];

         [UIView setAnimationsEnabled:NO];

         appDelegate.window.rootViewController = controller;

         [UIView setAnimationsEnabled:oldState];
     }
     completion:nil];
}

The code may look lengthy,but take it simple.You can only understand the logic if u do things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM