iOS UITabBarController";配置";图标颜色

iOS UITabBarController";配置";图标颜色,ios,uitabbarcontroller,uicolor,Ios,Uitabbarcontroller,Uicolor,我使用的是UITabBarController,我有7个选项卡,因此“更多”选项卡显示在UITabBarController中。我正在尝试将uitabar中所有选定的标题和图标的颜色更改为绿色,但是在配置屏幕中,我只设法更改标题颜色-图标保持为蓝色。如何将剩余的蓝色更改为绿色 以下是我在AppDelegate.m中的自定义设置 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDic

我使用的是
UITabBarController
,我有7个选项卡,因此“更多”选项卡显示在
UITabBarController
中。我正在尝试将
uitabar
中所有选定的标题和图标的颜色更改为绿色,但是配置屏幕中,我只设法更改标题颜色-图标保持为蓝色。如何将剩余的蓝色更改为绿色

以下是我在AppDelegate.m中的自定义设置

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [...]

    [[UITabBar appearance] setTintColor:[MyColor lightGreenColor]];
    [[UITabBar appearance] setSelectedImageTintColor:[MyColor lightGreenColor]];
    [[UINavigationBar appearance] setTintColor:[MyColor lightGreenColor]];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [MyColor lightGreenColor] } forState:UIControlStateSelected];

    return YES;
}
为了更好的理解


编辑 我使用Sen的方法,并将其与来自的
UIImage
类别结合起来。除此之外,我还必须为每个选项卡栏视图提供自己的标记Id,以便我可以将其用作图标图像数组的索引。这样,我就不必手动将图标涂成绿色,因此,我使用了一种非常有效的解决方法来解决图示问题。下面是我在
AppDelegate.m
中使用的代码片段。我期待着社区的改进

UIViewController *mainViewController = self.window.rootViewController;
if ([mainViewController isKindOfClass:[UITabBarController class]]) {
    UITabBarController *tabBarController = (UITabBarController *) mainViewController;

    if (tabBarController && tabBarController.tabBar &&
        [tabBarController.tabBar items] && [[tabBarController.tabBar items] count]) {
        NSArray *tabIcons = @[@"ICON1", @"ICON2", @"ICON3", ...];

        for (UITabBarItem *tabBarItem in [tabBarController.tabBar items]) {
            if (tabBarItem.tag < [tabIcons count]) {
                [tabBarItem setSelectedImage:[[UIImage imageNamed:[tabIcons objectAtIndex:tabBarItem.tag]
                                                        withColor:LIGHT_GREEN_COLOR]
                      imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
            }
        }
    }
}
UIViewController*mainViewController=self.window.rootViewController;
if([mainViewController是类的种类:[UITabBarController类]]){
UITabBarController*tabBarController=(UITabBarController*)主视图控制器;
if(tabBarController&&tabBarController.tabBar&&
[tabBarController.tabBar项目]&&&[tabBarController.tabBar项目]计数]){
NSArray*选项卡图标=@[@“ICON1”、@“ICON2”、@“ICON3”、…];
用于(在[tabBarController.tabBar项]中的UITabBarItem*tabBarItem){
如果(tabBarItem.tag<[tabIcons count]){
[tabBarItem setSelectedImage:[[UIImage ImageName:[Tabbarions对象索引:tabBarItem.tag]
withColor:浅绿色
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
}
}
}
}

您应该拥有具有正确颜色的图像,然后设置图标的图像

// unselected
[tabBarItem setImage: [img1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

// selected
[tabBarItem setSelectedImage: [img2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
应用程序:didFinishLaunchingWithOptions:
中执行此操作。您可以通过以下方式获得选项卡栏视图控制器:

UITabBarController * tc = (UITabBarController *)self.window.rootViewController;
您可以通过以下方式获得第i项:

UITabBarItem *tabBarItem = [tc.tabBar items][i];

这真的是唯一的可能性吗?这是非常低效的,会造成大量额外的工作量。为什么
uitabaritem
不继承全局色调?此外,我如何将您的建议与我的
AppDelegate.m
方法结合起来?根据我的经验,这是唯一的方法。您可以通过代码更改条形图的着色颜色和标题颜色,但我不认为您可以通过编程方式更改图标的颜色。我已经更新了我的答案。好的,谢谢你的更新!唯一让我困惑的是,其他图标(在正常部分,而不是更多部分)正确显示为绿色,而只有在配置屏幕中显示的图标是错误的。使用
self.window.rootViewController
方法,我将迭代所有选项卡栏图标?不仅仅是错误的?@user3869711您知道配置屏幕中特定标记的索引吗?我认为索引对我没有帮助,因为我基本上必须更改每个选项卡栏图标的图像(正常/选定)。这是因为可以切换“配置”屏幕中显示的选项卡栏。我希望现在你能理解,为什么我想避免这种方法,并找到一种更有效的方法。我就是不明白,为什么“配置”屏幕上的图标被涂成了错误的颜色。