How to change UITabBar Selection color

前端 未结 13 806
遇见更好的自我
遇见更好的自我 2020-12-13 09:02

I need to change the selection color of UITabBar from default blue to red. How do we do this.

相关标签:
13条回答
  • 2020-12-13 09:36

    iOS 5.0 fixes this issue but the solution is under NDA. Look up UITabBar in your documentation for an EASY way to do what you want to do.

    0 讨论(0)
  • 2020-12-13 09:39

    Because UITextAttributeTextColor is deprecated in iOS 7, you should use:

    [UITabBarItem.appearance setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor greenColor]} forState:UIControlStateNormal];    
    [UITabBarItem.appearance setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor purpleColor]} forState:UIControlStateSelected];
    
    0 讨论(0)
  • 2020-12-13 09:41

    The SDK does not make this easy, but it is technically possible. Apple apparently believes this to be part of their vision of a consistent look and feel.

    UITabBar is a subclass of UIView. You can always subclass and implement your own -drawRect:

    This is not a trivial task, however, you have to essentially re-implement the class from scratch or you risk some weird side-effects.

    0 讨论(0)
  • 2020-12-13 09:42

    It is extremely easy

    Create a custom class of UITabBarController and in -(void)viewDidLoad method add this line:

    [[self tabBar] setSelectedImageTintColor:[UIColor greenColor]]; 
    
    0 讨论(0)
  • 2020-12-13 09:43

    In iOS 7 it's simply the tintColor. One way to accomplish this could be to subclass UITabBarViewController, set the custom class in the storyboard, and in your viewDidLoad method of the subclassed tabBarVC add this:

    [[self tabBar] setTintColor:[UIColor redColor]];
    
    0 讨论(0)
  • 2020-12-13 09:45

    Starting from iOS 8 it's as simple as:

    UITabBar.appearance().tintColor = UIColor.redColor()
    
    0 讨论(0)
提交回复
热议问题