Comment changer la couleur de l'image de la barre d'onglet Pour sélectionné et non sélectionné

j'essaie de changer la couleur d'un élément de la barre d'onglets dans l'application iOS.

les contrôleurs child view sont intégrés dans un contrôleur de navigation.

j'ai ajouté ce code dans AppDelegate qui change simplement la couleur du texte.

 [UITabBarItem.appearance setTitleTextAttributes:
 @{NSForegroundColorAttributeName : [UIColor colorWithRed:38/255.0 green:54/255.0 blue:73/255.0 alpha:1.0]}
                                       forState:UIControlStateNormal];

 [UITabBarItem.appearance setTitleTextAttributes:@{NSForegroundColorAttributeName : kColorTabbarNavigationBarTint}
                                       forState:UIControlStateSelected];

et à l'intérieur des ressources de l'image,si je fais l'image particulière à rendre comme l'original il fonctionne, mais la couleur de sélection ne fonctionne pas pour l'image.

S'il vous plaît laissez-moi savoir ce qui pourrait être fait.

Merci d'avance...

0
demandé sur Vinayak GH 2015-10-27 14:11:09

1 réponses

essayez le code ci-dessous : quand vous voulez définir 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";

ou vous pouvez le régler dans storyboard, sélectionnez l'article de l'onglet particulier et voir ci-dessous

enter image description here

2
répondu VRAwesome 2015-11-17 10:51:42