混合使用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上很多應用程序幾乎都是以這種結構布局,實用性大,使用簡單。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM