UITabBarItem UIBarButtonItem改变title颜色

妖狐艹你老母 2022-08-21 05:51 196阅读 0赞
  1. iOS平台,UINavigationBar可以通过设置tintColor来改变导航条的背景颜色,但是由于UIBarButtonItem没有文本颜色设置功能,所以如果将UINavigationBartintColor设置成whiteColor的话,文字显示就不怎么清晰了。
  2.   这种情况网上一般建议通过建立一个UILabel,赋值给UINavigationItemtitleView属性,改变标题的颜色。建立一个UIButton,通过UIBarButtonIteminitWithCustomView方法创建UIBarButtonItem对象
  3.   效果不尽人意吧。当然可以通过设置背景图片什么的,加强效果。但总体来说不如只改变文本颜色方便。
  4.   iOSObjective C提供了runtime函数,定义在objc目录下面。通过这些运行时库函数可以对系统定义的对象进行修改,比如增加方法,修改方法的代码地址....通过枚举UINavigationBar的子视图,发现显示UIBarButtonItem内容的是UINavigationButton,它有一个子视图类型为UIButtonLabelUIButtonLabel继承自UILabelUIButtonLabel类型本身没有重载setTextColor:方法,因此调用class_addMethodUIButtonLabel类型增加一个setTextColor:方法,然后把传进来的color强制改成其他颜色,再调用UILabelsetTextColor:方法即可。
  5. 1. 有两件事情你需要做的是: 1)如果你想自定义的TabBar本身,你需要设置barTintColortabBarController // this will generate a black tab bar
  6. tabBarController.tabBar.barTintColor = [UIColor blackColor];
  7. // this will give selected icons and text your apps tint color
  8. tabBarController.tabBar.tintColor = appTintColor; // appTintColor is a UIColor *
  9. 2)设置tabBarItem文本外观要覆盖每一个状态:[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f],
  10. NSForegroundColorAttributeName : appTintColor
  11. } forState:UIControlStateSelected];
  12. // doing this results in an easier to read unselected state then the default iOS 7 one
  13. [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f],
  14. NSForegroundColorAttributeName : [UIColor colorWithRed:.5 green:.5 blue:.5 alpha:1]
  15. } forState:UIControlStateNormal];
  16. 2. 这个工作要在着色的标签栏不活跃的项目UITabBarItem *item = [self.tabBar.items objectAtIndex:1];
  17. //这里你需要用你想要的颜色的图标,因为它会被渲染,因为它是item.image = [[UIImage imageNamed:@"unselected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  18. //这个图标选中的标签,它会得到有色定义见self.tabBar.tintColor
  19. item.selectedImage = [UIImage imageNamed:@"selected.png"];
  20. 3. 使用self.tabBarController.tabBar.barStyle = UIBarStyleBlack;使标签栏黑色
  21. 4. 埃德的答案是完美的,但补充一点。 TabBar是默认的半透明使用TabBar下受到的观点的颜色(即的ViewController的视图的颜色影响的TabBar外观)。 所以我设置下面的代码不会受到影响。self.tabBarController.tabBar.translucent = false;
  22. 连同Ed的这里的答案是代码了。self.tabBarController.tabBar.barTintColor = [UIColor blackColor];
  23. self.tabBarController.tabBar.translucent = false;
  24. self.tabBarController.tabBar.tintColor = [UIColor blueColor];
  25. 5. 你试试吧for (UITabBarItem *item in self.tabBarController.tabBar.items) {
  26. item.image = [[UIImage imageNamed:@"youimage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  27. [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
  28. [UIColor whiteColor], UITextAttributeTextColor,
  29. nil] forState:UIControlStateNormal];
  30. item.selectedImage = [UIImage imageNamed:@"youimage.png"];
  31. }

发表评论

表情:
评论列表 (有 0 条评论,196人围观)

还没有评论,来说两句吧...

相关阅读