混合使用這兩個控件的好處是咱們能夠在NavigationBar添加更多的東西,如標題,按鈕等。讓用戶可以得到更多的信息。數組
UITabBarController的屬性ViewControllers接受以UIViewController或者UIViewController子類爲元素的數組。app
由於UINavigationController屬於UIViewController的子類,所以它固然就能夠成爲viewControllers的參數。佈局
先來看效果:spa
原理和以前文章所說的基本同樣:code
實現代碼: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