混合使用UITabBarController和UINavigationController

混合使用这两个控件的好处是咱们能够在NavigationBar添加更多的东西,如标题,按钮等。让用户可以得到更多的信息。数组

UITabBarController的属性ViewControllers接受以UIViewController或者UIViewController子类为元素的数组。app

由于UINavigationController属于UIViewController的子类,所以它固然就能够成为viewControllers的参数。布局

先来看效果:spa

image

原理和以前文章所说的基本同样:code

image

实现代码:blog

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //建立第一个视图控制器
    self.firstViewController = [[UIViewController alloc] init];
    self.firstViewController.view.backgroundColor = [UIColor brownColor];
    self.firstViewController.title = @"first view";
    self.firstViewController.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:
                                           UITabBarSystemItemDownloads tag:1];
    
    //建立第二个视图控制器
    self.secondViewController = [[UIViewController alloc] init];
    self.secondViewController.view.backgroundColor = [UIColor yellowColor];
    self.secondViewController.title = @"second view";
    self.secondViewController.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:2];
    
    //初始化第一个UINavigationController
    self.firstNavController = [[UINavigationController alloc]
                          initWithRootViewController:self.firstViewController];
    
    //初始化第二个UINavigationController
    self.secNavController = [[UINavigationController alloc]
                             initWithRootViewController:self.secondViewController];
    
    //初始化UITabBarController
    self.tabBarController = [[UITabBarController alloc] init];
    
    //为viewControllers添加引用
    self.tabBarController.viewControllers = @[self.firstNavController, self.secNavController];
    
    self.window.rootViewController = self.tabBarController;
    
    return YES;
}

在iPhone上不少应用程序几乎都是以这种结构布局,实用性大,使用简单。get

相关文章
相关标签/搜索