兩種常見的UITabBarController+UINavigationController模式分析比較

絕大部分軟件都採用了UITabBarController+UINavigationController的設計模式,這是一種很主流很經典的設計方式,而另一種UINavigationController+UITabBarController也是一種很是靈活的模式

方式一:TabBarController的viewControllers + 一個NavigationController

建立過程大概以下(關鍵代碼):

AppDelegate.m TabBarController *tabVC = [[TabBarController alloc]init]; //全局就這一個導航控制器 UINavigationController *nav =[[UINavigationController alloc] initWithRootViewController:tabVC]; self.window.rootViewController = nav; TabBarController.m - (void)viewDidLoad { [super viewDidLoad]; [self addChildVc:[[ChatViewController alloc] init] title:@"Chat" image:@"tabbar_home" selectedImage:@"tabbar_home_selected"]; [self addChildVc:[[ContactsListViewController alloc] init] title:@"Contact" image:@"tabbar_contacts" selectedImage:@"tabbar_contacts_selected"]; [self addChildVc:[[MoreViewController alloc] init] title:@"More" image:@"tabbar_more" selectedImage:@"tabbar_more_selected"]; [self addChildVc:[[ProfileViewController alloc] init] title:@"Profile" image:@"tabbar_profile" selectedImage:@"tabbar_profile_selected"]; } - (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage { childVc.title = title; childVc.tabBarItem.image = [UIImage imageNamed:image]; childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; [self addChildViewController:childVc]; }

方式二:TabBarController的viewControllers + 多個NavigationController

建立過程大概以下(關鍵代碼):

AppDelegate.m TabBarController *tabVC = [[TabBarController alloc]init]; self.window.rootViewController = tabVC; TabBarController.m - (void)viewDidLoad { [super viewDidLoad]; [self addChildVc:[[ChatViewController alloc] init] title:@"Chat" image:@"tabbar_home" selectedImage:@"tabbar_home_selected"]; [self addChildVc:[[ContactsListViewController alloc] init] title:@"Contact" image:@"tabbar_contacts" selectedImage:@"tabbar_contacts_selected"]; [self addChildVc:[[MoreViewController alloc] init] title:@"More" image:@"tabbar_more" selectedImage:@"tabbar_more_selected"]; [self addChildVc:[[ProfileViewController alloc] init] title:@"Profile" image:@"tabbar_profile" selectedImage:@"tabbar_profile_selected"]; } - (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage { childVc.title = title; childVc.tabBarItem.image = [UIImage imageNamed:image]; childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; // 每個控制器都是導航控制器 UINavigationViewController *navigationVc = [[UINavigationViewController alloc] initWithRootViewController:childVc]; [self addChildViewController:navigationVc]; }

對比分析

第一種方式:

導航控制器上的title不能和tabbar上面的同步,須要手動單獨設置,好比能夠在控制器的viewWillAppear:方法裏面設置.

第二種方式:

tabbar上的標題默認會直接同步到導航控制器上.

每一個界面都有本身的導航控制器, 界面跳轉都有本身的棧, 可能會更加靈活.

相關文章
相關標籤/搜索