How to change the tab bar image color for selected and unselected

后端 未结 1 748
眼角桃花
眼角桃花 2020-12-22 06:20

I\'m trying to change the color of a tab bar item in iOS app.

The child view controllers are embedded in an navigation controller.

I have added this code in

相关标签:
1条回答
  • 2020-12-22 07:02

    Try below code : When you want to set programatically.

    // get your TabBarController
    //UITabBarController *tabBarController = self.tabBarController
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController; 
    UITabBar *tabBar = tabBarController.tabBar;
    
    UITabBarItem *tabItem1 = [tabBar.items objectAtIndex:0];
    [tabItem1 setSelectedImage:[UIImage imageNamed:@"selectimg"]];
    tabItem1.selectedImage = [[UIImage imageNamed:@"selectimg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
    tabItem1.image = [[UIImage imageNamed:@"deselectimg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
    tabItem1.title = @"xxxx";
    
    UITabBarItem *tabItem2 = [tabBar.items objectAtIndex:1];
    tabItem2.selectedImage = [[UIImage imageNamed:@"selectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
    tabItem2.image = [[UIImage imageNamed:@"deselectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
    tabItem2.title = @"xxxx";
    
    UITabBarItem *tabItem3 = [tabBar.items objectAtIndex:2];
    tabItem3.selectedImage = [[UIImage imageNamed:@"selectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
    tabItem3.image = [[UIImage imageNamed:@"deselectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
    tabItem3.title = @"xxxx";
    
    UITabBarItem *tabItem4 = [tabBar.items objectAtIndex:3];
    tabItem4.selectedImage = [[UIImage imageNamed:@"selectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
    tabItem4.image = [[UIImage imageNamed:@"deselectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
    tabItem4.title = @"xxxx";
    

    Or you can set it in storyboard, Select particular tab item and see below

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