123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- //
- // JYTabBarController.m
- // JianYuIOS
- //
- // Created by apple on 2018/11/8.
- // Copyright © 2018年 lixianglan. All rights reserved.
- //
- #import "JYTabBarController.h"
- #import "JYNavigationController.h"
- #import "JYSearchViewController.h"
- #import "JYSubViewController.h"
- #import "JYExpViewController.h"
- #import "JYMineViewController.h"
- #import "ViewController1.h"
- #import "ViewController2.h"
- #import "ViewController3.h"
- #import "ViewController4.h"
- @interface JYTabBarController ()<UITabBarControllerDelegate>
- @property (nonatomic,strong)JYSearchViewController *V1;
- @property (nonatomic,strong)JYSubViewController *V2;
- @property (nonatomic,strong)JYExpViewController *V3;
- @property (nonatomic,strong)JYMineViewController *V4;
- @property (nonatomic,strong)ViewController1 *VV1;
- @property (nonatomic,strong)ViewController2 *VV2;
- @property (nonatomic,strong)ViewController3 *VV3;
- @property (nonatomic,strong)ViewController4 *VV4;
- @end
- @implementation JYTabBarController
- +(void)initialize
- {
-
- if (@available(iOS 9.0, *)) {
- UITabBarItem *tabBarItem = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[self]];
-
- NSMutableDictionary *dictNormal = [NSMutableDictionary dictionary];
- dictNormal[NSForegroundColorAttributeName] = UIColorFromRGB(0x888888);
- dictNormal[NSFontAttributeName] = [UIFont systemFontOfSize:10];
-
- NSMutableDictionary *dictSelected = [NSMutableDictionary dictionary];
- dictSelected[NSForegroundColorAttributeName] = UIColorFromRGB(0x2cb7ca);
- dictSelected[NSFontAttributeName] = [UIFont systemFontOfSize:10];
-
- [tabBarItem setTitleTextAttributes:dictNormal forState:UIControlStateNormal];
- [tabBarItem setTitleTextAttributes:dictSelected forState:UIControlStateSelected];
- } else {
- // Fallback on earlier versions
- }
-
-
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- self.delegate = self;
- self.tabBarController.tabBar.delegate = self;
- [self setUpAllChildVc];
- [self setSelectedIndex:0];
-
- [self switchRootViewController];
- }
- //判断是否跳转
- - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
- NSLog(@"点击了第几个item:%ld",tabBarController.tabBar.selectedItem.tag);
- // if (![LXUserDefaults token]) {
- // NSString *index = [NSString stringWithFormat:@"%ld",tabBarController.tabBar.selectedItem.tag];
- // [[NSNotificationCenter defaultCenter] postNotificationName:@"isLoginTabBar" object:nil userInfo:@{@"item_index":index}];
- // return NO;
- // }
- return YES;
-
- }
- - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
- // NSLog(@"点击了第几个item:%ld",item.tag);
- }
- #pragma mark - -----------------------------------------
- - (void)setUpAllChildVc
- {
- self.V1 = [[JYSearchViewController alloc] init];
- [self setUpOneChildVcWithVc:self.V1 Image:@"search" selectedImage:@"search1" title:@"搜索" tag:0];
- self.V2 = [[JYSubViewController alloc] init];
- [self setUpOneChildVcWithVc:self.V2 Image:@"subscribe" selectedImage:@"subscribe1" title:@"订阅" tag:1];
- self.V3 = [[JYExpViewController alloc] init];
- [self setUpOneChildVcWithVc:self.V3 Image:@"laboratory" selectedImage:@"laboratory1" title:@"实验室" tag:2];
- self.V4 = [[JYMineViewController alloc] init];
- [self setUpOneChildVcWithVc:self.V4 Image:@"mine" selectedImage:@"mine1" title:@"我的" tag:3];
-
-
- // self.VV1 = [[ViewController1 alloc] init];
- // [self setUpOneChildVcWithVc:self.VV1 Image:@"icon-home" selectedImage:@"icon-home2" title:@"搜索" tag:0];
- //
- // self.VV2 = [[ViewController2 alloc] init];
- // [self setUpOneChildVcWithVc:self.VV2 Image:@"icon-home" selectedImage:@"icon-home2" title:@"订阅" tag:1];
- //
- // self.VV3 = [[ViewController3 alloc] init];
- // [self setUpOneChildVcWithVc:self.VV3 Image:@"icon-home" selectedImage:@"icon-home2" title:@"实验室" tag:2];
-
- // self.VV4 = [[ViewController4 alloc] init];
- // [self setUpOneChildVcWithVc:self.VV4 Image:@"icon-home" selectedImage:@"icon-home2" title:@"我的" tag:3];
-
- }
- #pragma mark - 初始化设置tabBar上面单个按钮的方法
- /**
- *
- * 设置单个tabBarButton
- *
- * @param VC 每一个按钮对应的控制器
- * @param image 每一个按钮对应的普通状态下图片
- * @param selectedImage 每一个按钮对应的选中状态下的图片
- * @param title 每一个按钮对应的标题
- */
- - (void)setUpOneChildVcWithVc:(UIViewController *)VC Image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title tag:(NSInteger)current_tag
- {
- JYNavigationController *nav = [[JYNavigationController alloc] initWithRootViewController:VC];
- UIImage *myImage = [UIImage imageNamed:image];
- myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
-
- //tabBarItem,是系统提供模型,专门负责tabbar上按钮的文字以及图片展示
- VC.tabBarItem.image = myImage;
- UIImage *mySelectedImage = [UIImage imageNamed:selectedImage];
- mySelectedImage = [mySelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
- VC.tabBarItem.selectedImage = mySelectedImage;
- VC.tabBarItem.title = title;
- // VC.navigationItem.title = title;
- VC.tabBarItem.tag = current_tag;
- [self addChildViewController:nav];
- }
- -(UIColor *)randomColor
- {
- return [UIColor whiteColor];
- }
- #pragma mark - 根控制器的切换
- -(void)switchRootViewController
- {
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeItems:) name:@"changeItems" object:nil];
- }
- -(void)changeItems:(NSNotification *)notification
- {
-
- NSLog(@"收到改变tabbar的通知");
- NSDictionary *dict = notification.userInfo;
- NSInteger index = [dict[@"item_index"] integerValue];
- [self setSelectedIndex:index];
-
- // [self.V1 dealTabBar];
- // [self.V1 dealTabBar];
- // [self.V1 dealTabBar];
- // [self.V1 dealTabBar];
-
- if(index==1) {
- [self.V2 dealTabBar];
- }else if (index==2) {
- [self.V3 dealTabBar];
- }else if (index==3) {
- [self.V4 dealTabBar];
- }else {
-
- }
-
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- @end
|