混合使用UITabBarController和UINavigationController

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

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

因为UINavigationController属于UIViewController的子类,因此它当然就可以成为viewControllers的参数。

先来看效果:

image

原理和之前文章所说的基本一样:

image

实现代码:

- (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上很多应用程序几乎都是以这种结构布局,实用性大,使用简单。

原文地址:https://www.cnblogs.com/ai-developers/p/4517122.html