Change tintColor of unselected UITabBarController item title and background image

前端 未结 4 1886
心在旅途
心在旅途 2020-12-28 17:12

How can I change the tintColor of an unselected UITabBarItem title and background image iOS 8?

The default color for an unselected state is a light gray color, but

相关标签:
4条回答
  • 2020-12-28 17:21

    In your AppDelegate.m inside of application didFinishLaunchingWithOptions: use the following code:

    //unselected icon tint color 
    [[UIView appearanceWhenContainedIn:[UITabBar class], nil] setTintColor:[UIColor redColor]];
    
    //selected tint color 
    [[UITabBar appearance] setTintColor:[UIColor greenColor]];
    
    //text tint color 
    [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }
                                         forState:UIControlStateNormal];
    
    //background tint color 
    [[UITabBar appearance] setBarTintColor:[UIColor blueColor]];
    
    0 讨论(0)
  • 2020-12-28 17:29

    You can also render the image as original from the attributes inspector for the asset file without writing any code

    0 讨论(0)
  • 2020-12-28 17:40

    You can also set it up directly in Storyboard... Check my answer here: How to set UITabBarItem's unselected tint, ***including system items*** (iOS7)

    If you're using Storyboard you can also set both Image for Bar Item and Selected Image for Selected Bar Item to get unaltered image in tabBar.

    Alternatively in Assets catalog, you can select Render As: Original Image in the attributes of your image (View > Utilities > Show Attributes Inspector or shortcut ⌥⌘4 (Option + Command + 4))

    0 讨论(0)
  • 2020-12-28 17:44

    Figured it out!

    Use this to change the color of the text:

    [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor greenColor] }
                                             forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] }
                                             forState:UIControlStateSelected];
    

    And make sure that image rendering mode is set to ORIGINAL for the images

    UIImage *deselectedImage = [[UIImage imageNamed:@"deselectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *selectedImage = [[UIImage imageNamed:@"selectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    0 讨论(0)
提交回复
热议问题