修改UITabBarController字体颜色 图片大小颜色的小问题

1.有时候设置tabitem图片时,图片未选中时正常,选中后变的超大,这时就是需要我们给图片资源设置好原图和2x即可。

2.当我们用这串代码给tabbaritem设置图片时,会有问题发生,当选中后,图片保留的不是本身的颜色,而是默认的蓝色。

    UITabBarController *tabBarController = [[UITabBarController alloc]init];

    ViewController *viewController = [[ViewController alloc]init];

    viewController.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"" image:xx selectedImage:xx];

    tabBarController.viewControllers = @[[[UINavigationController alloc]initWithRootViewController:viewController],[[UINavigationController alloc]initWithRootViewController:homePageViewController2],[[UINavigationController alloc]initWithRootViewController:homePageViewController3]];



解决方法:

    UITabBarController *tabBarController = [[UITabBarController alloc]init];

ViewController *homePageViewController = [[ViewController alloc]init];

    

    ViewController *homePageViewController2 = [[ViewController alloc]init];

    

    ViewController *homePageViewController3 = [[ViewController alloc]init];

    

    ViewController *homePageViewController4 = [[ViewController alloc]init];

    

    ViewController *homePageViewController5 = [[ViewController alloc]init];

    

    tabBarController.viewControllers = @[[[UINavigationController alloc]initWithRootViewController:homePageViewController],[[UINavigationController alloc]initWithRootViewController:homePageViewController2],[[UINavigationController alloc]initWithRootViewController:homePageViewController3],[[UINavigationController alloc]initWithRootViewController:homePageViewController4],[[UINavigationController alloc]initWithRootViewController:homePageViewController5]];

    // 拿到 TabBar 在拿到想应的item

    UITabBar *tabBar = tabBarController.tabBar;

//设置被选中时显示是原图

    UITabBarItem *item0 = [tabBar.items objectAtIndex:0];

    item0.image = [[UIImage imageNamed:@"首页"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    item0.selectedImage = [[UIImage imageNamed:@"首页选"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    item0.title = @"首页";

    

    UITabBarItem *item1 = [tabBar.items objectAtIndex:1];

    item1.image = [[UIImage imageNamed:@"报表"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    item1.selectedImage = [[UIImage imageNamed:@"报表选"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    item1.title = @"店铺报表";

    

    UITabBarItem *item2 = [tabBar.items objectAtIndex:2];

    item2.image = [[UIImage imageNamed:@"论坛"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    item2.selectedImage = [[UIImage imageNamed:@"论坛选"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    item2.title = @"论坛";


    UITabBarItem *item3 = [tabBar.items objectAtIndex:3];

    item3.image = [[UIImage imageNamed:@"购物车"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    item3.selectedImage = [[UIImage imageNamed:@"购物车选"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    item3.title = @"购物车";

    

    UITabBarItem *item4 = [tabBar.items objectAtIndex:4];

    item4.image = [[UIImage imageNamed:@"个人中心"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    item4.selectedImage = [[UIImage imageNamed:@"个人中心选"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    item4.title = @"个人中心";


3.修改tab bar的title被选中后的颜色。ios7以前都是用

    UIColor *titleNormalColor = [UIColor colorWithRed:146 green:146 blue:146 alpha:1];

   

  [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:

                                                      titleNormalColor, UITextAttributeTextColor,

                                                      nil] forState:UIControlStateNormal];

   

    UIColor *titleHighlightedColor = [UIColor colorWithRed:226 green:6 blue:37 alpha:1];

   

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:

                                                      titleHighlightedColor, UITextAttributeTextColor,

                                                      nil] forState:UIControlStateSelected];


但是7以后 UITextAttributeTextColor 已经失效了,如今怎么使用还有待研究。

后来发现了个很简便的语句可以直接修改,简单粗暴!

// 拿到 TabBar 在拿到想应的item

    UITabBar *tabBar = tabBarController.tabBar;

    //修改字体颜色

    tabBar.tintColor = [UIColor redColor];

搞定!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值