IOS8 SplitVC + TabBarController + NavigationController

匿名 (未验证) 提交于 2019-12-03 02:20:02

问题:

I'm doing a universal App using size classes and I'm trying to use a SplitView with a TabBarController in the Master/Primary View. Before adding the splitView all worked fine, but now the App crashes (the reason depends on the hierarchy of the views).

So I tried the same storyboard starting from Apple SplitView template and add a TabBarController on its Master/primary view... same problem.

Hierarchy - Embedded master NavigationController in TabBarController: SplitVC (Master) > TabBarController > NavigationController > TableView SplitVC (Detail) > NavigationController > View

Added this code in AppDelegate.m (as seen here stackoverflow questions ios8-tabbarcontroller... to prevent DetailView being presented modally):

- (BOOL)splitViewController:(UISplitViewController *)splitViewController showDetailViewController:(UIViewController *)vc sender:(id)sender {         NSLog(@"UISplitViewController collapsed: %d", splitViewController.collapsed);      if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)     {         if (splitViewController.collapsed) {             UITabBarController *master = (UITabBarController *) splitViewController.viewControllers[0];             UINavigationController *masterNavigationController = (UINavigationController *)master.selectedViewController;             UINavigationController *destinationNavigationController = (UINavigationController *)vc;              // push detail view on the navigation controller             [masterNavigationController pushViewController:[destinationNavigationController.viewControllers lastObject] animated:YES];              return YES;         }     }      return NO; } 

It works fine... unless you simulate in iPhone6 Plus, in that case, after starting in portrait and selecting a row, if you rotate in landscape I see the detail view as primary AND secondary view.

Without adding this code in portrait orientation with iPhones the detail view is presented modally and of course without navigation buttons.

EDIT

After different tries and with some external helps I've made some steps forward the solution.

Short version (See Long Version to know why you have to do this)

A correct solution to the problem is to subclass TabBarController and make it support some methods:

@implementation MyTabBarController  - (void)showViewController:(UIViewController *)vc sender:(id)sender {     if ([self.selectedViewController isKindOfClass:UINavigationController.class])         [self.selectedViewController showViewController:vc sender:sender];     else         [super showViewController:vc sender:sender]; }  - (UIViewController*)separateSecondaryViewControllerForSplitViewController:(UISplitViewController *)splitViewController {     return [self.selectedViewController separateSecondaryViewControllerForSplitViewController:splitViewController]; }  - (void)collapseSecondaryViewController:(UIViewController *)secondaryViewController forSplitViewController:(UISplitViewController *)splitViewController {     [self.selectedViewController collapseSecondaryViewController:secondaryViewController forSp
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!