两种常见的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上的标题默认会直接同步到导航控制器上.
每个界面都有自己的导航控制器, 界面跳转都有自己的栈, 可能会更加灵活.

转载于:https://www.cnblogs.com/Mr-zyh/p/8529770.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值