【问题标题】:iOS: Tabbar Tint ColoriOS:标签栏色调颜色
【发布时间】:2014-05-05 01:07:42
【问题描述】:

我正在代码中创建一个 TabBar:

self = [super init];

    self.tabBarController = [[UITabBarController alloc]init];
    self.tabBarController.delegate = self;
    self.tabBarController.navigationController.delegate = self;

    //Build Model
    NSArray *topLevelControllers = [self buildTopLevelControllers];
    [self.tabBarController setViewControllers:topLevelControllers animated:NO];

    //Inbox will be lead navigation
    self.tabBarController.selectedIndex = kSelectedIndex;

    [self.view addSubview:self.tabBarController.view];
    self.tabBarController.customizableViewControllers = @[];


    return self;
}

在 App Delegate 中,我有以下 Tint 代码:

[[UIView appearance] setTintColor:[ColorUtils globalTintColor]];

问题: 应用启动时,Tab Bar 中的所有图标都带有全局颜色。当我选择一个然后取消选择它时,它们会返回灰色(图像默认颜色)。

预期结果: 应用启动时,所有按钮均为灰色(灰色是 PNG 中的图像颜色)。当我点击标签栏图标时,颜色变为全局色调颜色。

尝试过:在 App 委托中,我添加了以下代码,但它不起作用:

TabBarVC *tabBarVC = [[TabBarVC alloc]init];
tabBarVC.tabBarController.tabBar.tintColor = [UIColor greyColor];
tabBarVC.tabBarController.tabBar.selectedImageTintColor = [STColorUtils globalTintColor];

[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [ColorUtils globalTintColor]}];


[[UIView appearance] setTintColor:[ColorUtils globalTintColor]];
[self.window setRootViewController:tabBarVC];

但是,如果我注释掉:

//[[UIView appearance] setTintColor:[ColorUtils globalTintColor]];

图标确实显示为灰色,但全局色调颜色为 iOS7 默认值:蓝色。

【问题讨论】:

    标签: ios7 uitabbarcontroller xcode5 tintcolor


    【解决方案1】:

    iOS 7 中的 selectedImageTintColor 存在一个已知问题。最后我检查了该问题尚未解决。所以删除 -

    tabBarVC.tabBarController.tabBar.selectedImageTintColor = [STColorUtils globalTintColor];
    

    你也想使用 UITabBar 的外观所以替换

    [[UIView appearance] setTintColor:[ColorUtils globalTintColor]];
    

    [[UITabBar appearance] setTintColor:[ColorUtils globalTintColor]];
    

    【讨论】:

    • 我还必须包括:[[UINavigationBar appearance] setTintColor:[ColorUtils globalTintColor]];请调整您的答案以供其他人查看。谢谢。
    【解决方案2】:

    这是迄今为止我找到的最佳解决方案:

    [UIView appearance].tintColor = [UIColor redColor];
    
    // the selected image and text will still use UIView.tintColor. 
    // this is to tint the unselected images until they are tapped
    // [UIColor grayColor] does not exactly match the default color, but it's subtle
    [UIView appearanceWhenContainedIn:[UITabBar class], nil].tintColor = [UIColor grayColor];
    

    当使用 swift 时,我需要创建一个包含 +(UIView *)viewAppearanceWhenContainedInTabBar() 方法的 Objective-c 文件来使用 [UIView appearanceWhenContainedIn:] 方法,因为它不适用于 swift :(

    【讨论】:

      【解决方案3】:

      更改 UIWindow 的 tintColor 属性。它应用于添加到此窗口的每个 UIView。

      【讨论】:

        猜你喜欢
        • 2013-09-18
        • 2013-09-27
        • 2013-06-11
        • 2015-07-13
        • 1970-01-01
        • 2016-06-15
        • 2015-07-11
        • 1970-01-01
        • 2015-07-14
        相关资源
        最近更新 更多