ios - 带有 Storyboard的通用应用程序上的 UISplitViewController

标签 ios uistoryboard uisplitviewcontroller

我想制作一个在 iPad 上使用 UISplitViewControler 的应用程序(据我所知它只在 iPad 上可用),但我希望该应用程序是通用的。

设置是这样的:

我有一个 UITableView(作为主视图),当我选择一行时,它应该显示该单元格的详细 View 。我正在使用 Storyboard,但我不知道如何只为 iPad 实现 Split View。

实现该目标的最简单方法是什么?谢谢。

最佳答案

你不需要两个 Storyboard来做到这一点。你可以在一个 Storyboard中同时使用它们。对于 iphone,我们通常使用一个类 SWRevealViewController (如果您是 iOS 编码的新手 ..:))用于 ipad 的侧边菜单和 splitviewcontroller。我们也可以为 ipad 使用 SWRevealViewController。这取决于您的要求。

对于通用应用程序,使用 size Classes 创建 View Controller (通常我们对通用应用程序使用任意高度任意宽度)。

更改这些尺寸类别并根据需要为 ipad 和 iphone 创建不同的 View Controller 。在大多数情况下,任何高度任何宽度都可以完成这项工作。

创建 View Controller 后,在 appdelegate 中,使用 instantiateViewcontrollerWithIdentifier 方法,加载所需的 View Controller 。

    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.
}

对于 ipad,加载 splitviewcontroller。,对于 iPhone,加载 swrevealviewcontroller。

这是核心基础知识。如果您需要更多信息,请告诉我。

EDIT

你有没有看到 Storyboard中初始 VC( View Controller )的箭头标记?这个 vc 在启动屏幕之后首先加载。在我的应用程序中,我有一个主屏幕,这对 iphone 和 ipad 都是通用的(使用大小类如上所述)。所以我可以将这个 vc 设置为初始 VC。在这种情况下,我不必在 appdelegate 中做任何事情。但是如果我有一个不同的 ipad 主屏幕,那么我可以进行条件检查appdelegate didFinishLaunchingWithOptions

您可以像这样加载第一个屏幕。您应该按照 splitVC 教程和 swrevealcontroller 教程来设置侧边菜单。只有当第一个屏幕包含侧边菜单时,您才应该加载 SWrevealVC 或 splitViewcontroller。

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

代码看似冗长,但要简单,只有自己动手才能明白其中的逻辑。

关于ios - 带有 Storyboard的通用应用程序上的 UISplitViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33052961/

相关文章:

iphone - 如何为我自己的按钮/导航栏项目使用尖头后退按钮?

ios - 了解何时通过手势关闭分屏 View Controller 的主控

iOS:UILabel 在 View 中居中

iphone - 如何将为iPhone创建的项目更改为ipad(通用)?

ios - 如何将 SOAP 响应放入 UITableView?

ios - 保留循环 Swift 闭包

iphone - 如何延迟执行 Segue

ios - 使用 Storyboard导航一组复杂的 View ,展开 segue 是不够的

ios - Facebook 应用程序重定向到 App Store

xcode - 如何从rootViewController中的多级导航更新detailViewController.detailItem