iOS8下的UISplitViewController初探
UISplitViewController一直都是iPad的专利。但iOS8对此做出了改变,它使得这种分层级的应用,更加Universe了,一次编码,解决多屏、多设备的兼容问题。
下面来看下新的API如何使用吧。
我热衷于代码创建VC,所以喜欢xib的同学就忍一忍吧~
在AppDelegate中创建splitview:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; self.splitViewController = [[UISplitViewController alloc] init]; MainViewController *controller = [[MainViewController alloc] init]; UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller]; DetailViewController *detailController = [[DetailViewController alloc] init]; UINavigationController *navDetailController = [[UINavigationController alloc] initWithRootViewController:detailController]; controller.navDetailController = navDetailController; self.splitViewController.viewControllers = @[navController,navDetailController]; self.window.rootViewController = self.splitViewController; [self.window makeKeyAndVisible]; return YES; }
在MainViewController中,添加tableview,而在tableview的点击事件中,添加如下代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ DetailViewController *detailViewController = [self.navDetailController.viewControllers objectAtIndex:0]; detailViewController.textLabel.text = [NSString stringWithFormat:@"click at cell index: %d",indexPath.row+1]; [self.splitViewController showDetailViewController:self.navDetailController sender:nil]; }
最后一句[self.splitViewControllershowDetailViewController:self.navDetailControllersender:nil];这是iOS8新提供的API,给我感觉就是像navigationController一样的地位。
通过这样,我们不必再区分UIInterfaceOrientation以及UIUserInterfaceIdiom了。同一套代码,在iPhone和iPad下的允许如下图:
iPhone:
----》》
iPad:
具体的还得继续细究。