UITabBarController+UINavigationController 进入应用只显示一个tab的解决方法
来源:互联网 发布:java难学吗 编辑:程序博客网 时间:2024/05/17 06:15
各个tab所对应的controller的tabBarItem,与tabBarController的tabBar上面的item不对应的问题吧。在初始化的时候,每个viewController的title及对应的tab上图片还没有初始化好,所以获取不到,就没法显示。
脑子比较乱,就记下来吧,以后就可以直接抄了!!!
现在有下面几种写法:
写法一
NSArray *nameArray = @[@"首页", @"公告", @"月报推送", @"热点", @"我的"];NSArray *imageArray = @[@"icon_index", @"icon_notice", @"icon_push", @"icon_hot", @"icon_mine"];NSArray *selectedImageArray = @[@"icon_index_s", @"icon_notice_s", @"icon_push_s", @"icon_hot_s", @"icon_mine_s"];NSArray *viewControllerArray = @[[[HomeViewController alloc] init], [[GongGaoViewController alloc] init], [[MonthlyPushViewController alloc] init], [[HotSpotViewController alloc] init], [[MineViewController alloc] init]];ZZZBaseTabBarController *tabBarController = [[ZZZBaseTabBarController alloc] init];// 使用for循环生成各个tab对应的内容for (int index = 0; index < nameArray.count; index ++) { UIViewController *viewController = viewControllerArray[index]; ZZZBaseNavigationController *navigationController = [[ZZZBaseNavigationController alloc] initWithRootViewController:viewController]; navigationController.tabBarItem = [[UITabBarItem alloc] initWithTitle:nameArray[index] image:[UIImage imageNamed:imageArray[index]] selectedImage:[UIImage imageNamed:selectedImageArray[index]]]; //设置文字偏移 [navigationController.tabBarItem setTitlePositionAdjustment:UIOffsetMake(2, -4)]; [tabBarController addChildViewController:navigationController];}self.window.rootViewController = tabBarController;[self.window makeKeyAndVisible];
其实后面的写法都一样,就是有的展开,有的用循环的区别吧
写法二
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];// 设置五个viewController,直接对viewController的tabBarItem属性赋值,同时需要设置viewController的title,否则在tabBarController初始化时,对应的这些title是取不到的,所以看不到对应的文字 HomeViewController *homeViewController = [[HomeViewController alloc] init];homeViewController.title = @"首页";homeViewController.tabBarItem.image=[UIImage imageNamed:@"icon_index"];homeViewController.tabBarItem.selectedImage = [UIImage imageNamed:@"icon_index_s"];homeViewController.tabBarItem.badgeValue=@"123";GongGaoViewController *gonggaoViewController = [[GongGaoViewController alloc] init];gonggaoViewController.title = @"公告";gonggaoViewController.tabBarItem.image=[UIImage imageNamed:@"icon_notice"];gonggaoViewController.tabBarItem.selectedImage = [UIImage imageNamed:@"icon_notice_s"];MonthlyPushViewController *monthPushViewController = [[MonthlyPushViewController alloc] init];monthPushViewController.title = @"推送";monthPushViewController.tabBarItem.image=[UIImage imageNamed:@"icon_push"];monthPushViewController.tabBarItem.selectedImage = [UIImage imageNamed:@"icon_push_s"];HotSpotViewController *hotSpotViewController = [[HotSpotViewController alloc] init];hotSpotViewController.title = @"热点";hotSpotViewController.tabBarItem.image=[UIImage imageNamed:@"icon_hot"];hotSpotViewController.tabBarItem.selectedImage = [UIImage imageNamed:@"icon_hot_s"];MineViewController *mineViewController = [[MineViewController alloc] init];mineViewController.title = @"我的";mineViewController.tabBarItem.image=[UIImage imageNamed:@"icon_mine"];mineViewController.tabBarItem.selectedImage = [UIImage imageNamed:@"icon_mine_s"];// 五个navigationControllerZZZBaseNavigationController *homeNavCtrl = [[ZZZBaseNavigationController alloc] initWithRootViewController:homeViewController];ZZZBaseNavigationController *gonggaoNavCtrl = [[ZZZBaseNavigationController alloc] initWithRootViewController:gonggaoViewController];ZZZBaseNavigationController *monthPushNavCtrl = [[ZZZBaseNavigationController alloc] initWithRootViewController:monthPushViewController];ZZZBaseNavigationController *hotSpotNavCtrl = [[ZZZBaseNavigationController alloc] initWithRootViewController:hotSpotViewController];ZZZBaseNavigationController *mineNavCtrl = [[ZZZBaseNavigationController alloc] initWithRootViewController:mineViewController];// 设置tabBarController和子viewController ZZZBaseTabBarController *tabBarController = [[ZZZBaseTabBarController alloc] init];tabBarController.viewControllers = @[homeNavCtrl, gonggaoNavCtrl, monthPushNavCtrl, hotSpotNavCtrl, mineNavCtrl];self.window.rootViewController = tabBarController;[self.window makeKeyAndVisible];
写法三
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];// 设置五个viewController,及其对应的tabBarItem(使用alloc的方法新建,再赋值) HomeViewController *homeViewController = [[HomeViewController alloc] init];UITabBarItem *homeTabBarItem = [[UITabBarItem alloc] initWithTitle:@"AA" image: [UIImage imageNamed:@"icon_index"] selectedImage:[UIImage imageNamed:@"icon_index_s"]];homeViewController.tabBarItem = homeTabBarItem;homeViewController.tabBarItem.badgeValue=@"123";GongGaoViewController *gonggaoViewController = [[GongGaoViewController alloc] init];UITabBarItem *gonggaoTabBarItem = [[UITabBarItem alloc] initWithTitle:@"BB" image: [UIImage imageNamed:@"icon_notice"] selectedImage:[UIImage imageNamed:@"icon_notice_s"]];gonggaoViewController.tabBarItem = gonggaoTabBarItem;MonthlyPushViewController *monthPushViewController = [[MonthlyPushViewController alloc] init];UITabBarItem *pushTabBarItem = [[UITabBarItem alloc] initWithTitle:@"CC" image: [UIImage imageNamed:@"icon_push"] selectedImage:[UIImage imageNamed:@"icon_push_s"]];monthPushViewController.tabBarItem = pushTabBarItem;HotSpotViewController *hotSpotViewController = [[HotSpotViewController alloc] init]; UITabBarItem *hotTabBarItem = [[UITabBarItem alloc] initWithTitle:@"DD" image: [UIImage imageNamed:@"icon_hot"] selectedImage:[UIImage imageNamed:@"icon_hot_s"]];hotSpotViewController.tabBarItem = hotTabBarItem;MineViewController *mineViewController = [[MineViewController alloc] init];UITabBarItem *mineTabBarItem = [[UITabBarItem alloc] initWithTitle:@"EE" image: [UIImage imageNamed:@"icon_mine"] selectedImage:[UIImage imageNamed:@"icon_mine_s"]];mineViewController.tabBarItem = mineTabBarItem; // 五个navigationControllerZZZBaseNavigationController *homeNavCtrl = [[ZZZBaseNavigationController alloc] initWithRootViewController:homeViewController];ZZZBaseNavigationController *gonggaoNavCtrl = [[ZZZBaseNavigationController alloc] initWithRootViewController:gonggaoViewController];ZZZBaseNavigationController *monthPushNavCtrl = [[ZZZBaseNavigationController alloc] initWithRootViewController:monthPushViewController];ZZZBaseNavigationController *hotSpotNavCtrl = [[ZZZBaseNavigationController alloc] initWithRootViewController:hotSpotViewController];ZZZBaseNavigationController *mineNavCtrl = [[ZZZBaseNavigationController alloc] initWithRootViewController:mineViewController];// 设置tabBarController和子viewControllerZZZBaseTabBarController *tabBarController = [[ZZZBaseTabBarController alloc] init];tabBarController.viewControllers = @[homeNavCtrl, gonggaoNavCtrl, monthPushNavCtrl, hotSpotNavCtrl, mineNavCtrl];self.window.rootViewController = tabBarController;[self.window makeKeyAndVisible];
阅读全文
0 0
- UITabBarController+UINavigationController 进入应用只显示一个tab的解决方法
- IOS UINavigationController UITabBarController嵌套使用的时候显示消息数量
- UITabBarController:tab bar 的 image 太大、不清晰的解决方法
- UINavigationController和UITabBarController的集成
- UITabBarController 和 UINavigationController 的 popToRootViewController
- 继承UINavigationController的问题----UITabBarController
- UINavigationController与UITabbarController的样式
- UINavigationController,UITabBarController
- UINavigationController, UITabBarController
- UINavigationController+UITabBarController
- UINavigationController、UITabBarController
- TextView中的连续空格只显示一个的解决方法
- TextView中的连续空格只显示一个的解决方法
- 【iOS开发-UITabBarController】UITabBarController上面的UINavigationController的设置
- UITabBarController的使用方法 UINavigationController的使用方法
- 关于StoryBoard的使用(3)两个添加一个UINavigationController||UITabBarController最基本的使用
- iphone--UIViewController/UITabBarController和UINavigationController的结合
- UINavigationController和UITabBarController共存的例子
- SQL语句优化技巧
- Linux系统安全加固设置详细教程
- Laravel-excel安装及使用
- HDU
- 简单用Maven找所需的JAR包,放自己的包
- UITabBarController+UINavigationController 进入应用只显示一个tab的解决方法
- Android 学习文档整理收集
- 【JavaMail】(2)JavaMail的一个案例:用户在网站上注册完成后给用户发一封邮件, 且需要用户通过邮件激活后才可以使用。
- TCP协议如何保证传输的可靠性
- apue3 搭建
- hdu 1542 Atlantis (线段树之扫描线)
- c++ primer plus阅读笔记3---指针问题
- BOM弹窗
- android:configChanges属性值的含义