Custom tab bar icon colors

后端 未结 7 1138
傲寒
傲寒 2020-12-08 12:39

Im currently using Xcode 5 to develop a list oriented app. I have a custom tint for the tab bar, custom images for the tab icons, custom tint for the tab bar\'s icon images

相关标签:
7条回答
  • 2020-12-08 12:50

    You can do this purely from the storyboard without writing any code by adding a "User Defined Runtime Attribute":

    1. Select your UITabViewController in the storyboard
    2. Open the "Document Outline" and make sure that you select the "Tab Bar" view in the scene.
    3. Show the "Identity Inspector". You should see a section for "User Defined Runtime Attributes"
    4. Add the following:
      • Key Path: tintColor
      • Type: Color
      • Value: Select the color you want.
    0 讨论(0)
  • 2020-12-08 12:50

    Try this way..it worked for me

    In app delegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    
    
     UITabBarController *tabBarController=(UITabBarController*)(self.window.rootViewController);
        UITabBar *tabBar=tabBarController.tabBar;
      UITabBarItem *tabBarItem1=[[tabBar items] objectAtIndex:0];//first tab bar
     [tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"yourImageSelected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"yourImageUnselected.png"]];//image should be 30 by 30
    }
    

    run and go

    0 讨论(0)
  • 2020-12-08 12:54

    You can try this to tint selected icon :

    // Custom the tab bar
    [[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
    

    and this to tint the non active icon :

    [self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"item_seleted.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"item_unselected.png"]];
    
    0 讨论(0)
  • 2020-12-08 13:00

    You need to set the rendering mode for each tab's (unselected) image to UIImageRenderingModeAlwaysOriginal. So, in your app delegate, get a reference to the tab bar and then iterate over each tab bar item, adjusting the image modes.

    There's probably a better way to get a reference to the tab bar, but I did the following:

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UITabBarController *tbc = [sb instantiateInitialViewController];
    self.window.rootViewController = tbc;
    UITabBar *tb = tbc.tabBar;
    

    Then the image adjustment can be done as follows:

    NSArray *items = tb.items;
    
    for (UITabBarItem *tbi in items) {
        UIImage *image = tbi.image;
        tbi.selectedImage = image;
        tbi.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    
    0 讨论(0)
  • 2020-12-08 13:00

    Setting Custom Tabbar with selected and non-seleted Image. Also having tabbarItem Image Insets position in center

    UITabBar *tabBar = self.tabBarController.tabBar;
    
    UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
    UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
    UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
    UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
    
    [item0 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue.png"]  withFinishedUnselectedImage:[UIImage imageNamed:@"iconGray.png"] ];
    [item1 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue2.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"icon-2.png"]];
    [item2 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue3.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"icon-3.png"]];
    [item3 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue4.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"icon-4.png"]];
    
    item0.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
    item1.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
    item2.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
    item3.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
    

    **In viewWillAppear method of first viewcontroller. **

    0 讨论(0)
  • 2020-12-08 13:07

    Because setFinishedSelectedImage:withFinishedUnselectedImage is deprecated, I used an altered version of Ram S's answer by replacing:

    [item0 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue.png"]  withFinishedUnselectedImage:[UIImage imageNamed:@"iconGray.png"] ];
    

    with:

    [item0 setImage:[[UIImage imageNamed:@"iconGray.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [item0 setSelectedImage:[[UIImage imageNamed:@"iconBlue.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    

    See UITabBarItem setFinishedSelectedImage: deprecated in iOS7 for more information.

    0 讨论(0)
提交回复
热议问题