UITabBarItem UIBarButtonItem改变title颜色

在iOS平台,UINavigationBar可以通过设置tintColor来改变导航条的背景颜色,但是由于UIBarButtonItem没有文本颜色设置功能,所以如果将UINavigationBar的tintColor设置成whiteColor的话,文字显示就不怎么清晰了。

  这种情况网上一般建议通过建立一个UILabel,赋值给UINavigationItem的titleView属性,改变标题的颜色。建立一个UIButton,通过UIBarButtonItem的initWithCustomView方法创建UIBarButtonItem对象

  效果不尽人意吧。当然可以通过设置背景图片什么的,加强效果。但总体来说不如只改变文本颜色方便。

  iOS的Objective C提供了runtime函数,定义在objc目录下面。通过这些运行时库函数可以对系统定义的对象进行修改,比如增加方法,修改方法的代码地址....通过枚举UINavigationBar的子视图,发现显示UIBarButtonItem内容的是UINavigationButton,它有一个子视图类型为UIButtonLabel,UIButtonLabel继承自UILabel,UIButtonLabel类型本身没有重载setTextColor:方法,因此调用class_addMethod给UIButtonLabel类型增加一个setTextColor:方法,然后把传进来的color强制改成其他颜色,再调用UILabel的setTextColor:方法即可。

1. 有两件事情你需要做的是: 1)如果你想自定义的TabBar本身,你需要设置barTintColor为tabBarController: // this will generate a black tab bar
tabBarController.tabBar.barTintColor = [UIColor blackColor];

// this will give selected icons and text your apps tint color
tabBarController.tabBar.tintColor = appTintColor; // appTintColor is a UIColor *

2)设置tabBarItem文本外观要覆盖每一个状态:[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f],
NSForegroundColorAttributeName : appTintColor
} forState:UIControlStateSelected];

// doing this results in an easier to read unselected state then the default iOS 7 one
[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f],
NSForegroundColorAttributeName : [UIColor colorWithRed:.5 green:.5 blue:.5 alpha:1]
} forState:UIControlStateNormal];

2. 这个工作要在着色的标签栏不活跃的项目UITabBarItem *item = [self.tabBar.items objectAtIndex:1];

//这里你需要用你想要的颜色的图标,因为它会被渲染,因为它是item.image = [[UIImage imageNamed:@"unselected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

//这个图标选中的标签,它会得到有色定义见self.tabBar.tintColor
item.selectedImage = [UIImage imageNamed:@"selected.png"];

3. 使用self.tabBarController.tabBar.barStyle = UIBarStyleBlack;使标签栏黑色 

4. 埃德的答案是完美的,但补充一点。 的TabBar是默认的半透明使用TabBar下受到的观点的颜色(即的ViewController的视图的颜色影响的TabBar外观)。 所以我设置下面的代码不会受到影响。self.tabBarController.tabBar.translucent = false;

连同Ed的这里的答案是代码了。self.tabBarController.tabBar.barTintColor = [UIColor blackColor];
self.tabBarController.tabBar.translucent = false;
self.tabBarController.tabBar.tintColor = [UIColor blueColor];

5. 你试试吧for (UITabBarItem *item in self.tabBarController.tabBar.items) {
item.image = [[UIImage imageNamed:@"youimage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
nil] forState:UIControlStateNormal];

item.selectedImage = [UIImage imageNamed:@"youimage.png"];
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值