tabBar 选中默认蓝色 ,取消选中(自定义)

时间:2023-03-08 19:53:41

- (void)viewDidLoad {

[super viewDidLoad];

//    [self _initSubViewControllers];

//    [self _customTabBar];

//    [self.navigationItem setRightBarButtonItem:UIBarStyleDefault];

//    self.SerBar=[[UISearchBar alloc]initWithFrame:CGRectMake(self.view.frame.size.width-150, 20, 120, 40)];

//    [self.SerBar setPlaceholder:@"search"];

//    [self.SerBar setPrompt:@"搜索"];//顶部提示文本,title

//    [self.SerBar setBarStyle:UIBarStyleBlackTranslucent];//搜索框样式

//    [self.SerBar setTintColor:[UIColor whiteColor]];

//    [self.SerBar setShowsBookmarkButton:NO];

//    [self.SerBar setTranslucent:YES];//透明

//    [self.view addSubview:self.SerBar];

////    方法二

NSMutableDictionary *attrs=[NSMutableDictionary dictionary];

//设置字体大小

attrs[NSFontAttributeName]=[UIFont systemFontOfSize:13];

//设置字体颜色

attrs[NSForegroundColorAttributeName]=[UIColor colorWithRed:0.4 green:0.4 blue:0.6 alpha:1];

//初始化一个字典

NSMutableDictionary *Selectedattrs=[NSMutableDictionary dictionary];

//设置字体大小

Selectedattrs[NSFontAttributeName]=attrs[NSFontAttributeName];

//设置字体颜色

Selectedattrs[NSForegroundColorAttributeName]=[UIColor colorWithRed:0.3 green:0.5 blue:0.6 alpha:1];

UITabBarItem *item=  [UITabBarItem appearance];

[item setTitleTextAttributes:attrs forState:UIControlStateNormal];

[item setTitleTextAttributes:Selectedattrs forState:UIControlStateSelected];

//添加子控制器

[self setupchidVc:[HomeViewController new] title:@"主页" image:@"-1.png" selectedimage:@"T1"];

[self setupchidVc:[DSDieQuoteViewController new] title:@"模报" image:@"-2.png" selectedimage:@"T2"];

[self setupchidVc:[WXViewController new]  title:@"校谈" image:@"-3.png" selectedimage:@"T3"];

}

//-(void)_customTabBar{

//

//    for (UIView *subView in self.tabBar.subviews) {

//        //        移除

//        [subView removeFromSuperview];

//    }

//    底部图片

//    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"1.111png"]];

//    //    创建背景选中的图片

//    _selectV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 42, 42)];

////    按钮图片

//    _selectV.image = [UIImage imageNamed:@"selectTabbar_bg_all.png"];

//

//    [self.tabBar addSubview:_selectV];

// 创建标签栏按钮

//    获取所有按钮的图片

//    NSArray *imageNames = @[@"-1.png",@"-2.png",@"-3.png"];

//    NSArray *titles = @[@"主页",@"模报",@"校谈"];

// 创建按钮

//    float button_width = self.view.frame.size.width / imageNames.count;

//    for (int i = 0; i < imageNames.count; i++) {

//        // 创建按钮

//        DSTabBarItem *item = [[DSTabBarItem alloc] initWithFrame:CGRectMake(i * button_width, 0, button_width, 49)];

//        // 设置tag

//        item.tag = i;

//        // 设置标题

//        item.titleName = titles[i];

//        // 设置图片

//        item.imageName = imageNames[i];

//        // 添加事件

//        [item addTarget:self action:@selector(itemAction:) forControlEvents:UIControlEventTouchUpInside];

//

//        [self.tabBar addSubview:item];

// 如果当前创建的是第一个按钮

//        if (i == 0) {

//            _selectV.center = item.center;

//        }

//    }

//}

-(void)setupchidVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image  selectedimage:(NSString *)selectedimage

{

vc.tabBarItem.title=title;

vc.tabBarItem.image=[UIImage imageNamed:image];

vc.tabBarItem.selectedImage=[UIImage imageNamed:selectedimage];

[self addChildViewController:vc];

//取消选中时的背景色

vc.tabBarItem.selectedImage = [vc.tabBarItem.selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

}