【问题标题】:How to set UITabBarItem's unselected tint, ***including system items*** (iOS7)如何设置UITabBarItem选择的色调,***包括系统项目***(iOS7)
【发布时间】:2013-10-01 19:57:22
【问题描述】:

(注意:我看到关于 SO 有几个类似的问题,但它们似乎都没有解决我想要更改自定义和系统 UITabBarItems 的未选择外观的具体问题。)

我在 iOS7 中工作。我有一个带有一些按钮的 UITabBar。其中一些是我的按钮,一些是系统按钮。示例:

UITabBarItem *searchButton = [[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemSearch    tag: navSearchItem];
UITabBarItem *bookMkButton = [[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemBookmarks tag: navBookmarksItem];
UITabBarItem *homeButton   = [[UITabBarItem alloc] initWithTitle: @"Home"     image: [self tabBarImageNamed: @"home-tab"]     tag: navHomeItem];
UITabBarItem *setingButton = [[UITabBarItem alloc] initWithTitle: @"Settings" image: [self tabBarImageNamed: @"settings-tab"] tag: navSettingsItem];

navTabBar.items = @[ searchButton, homeButton, bookMkButton, setingButton];

我可以很容易地设置选定按钮的色调,使用:

[[UITabBar appearance] setSelectedImageTintColor: MY_FAVORITE_COLOR];

我可以将未选中的按钮文本颜色设置为白色:

[[UITabBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], UITextAttributeTextColor, nil]
                                         forState: UIControlStateNormal];

我想要做的是将 UNSELECTED 按钮的图像色调设置为白色。我可以很容易地设置未选择的图像,但这仅适用于 my 按钮。我也想为系统按钮执行此操作。也就是说,我希望我的搜索和书签按钮也是未选中的白色。

如果我尝试重新创建他们的图标来制作自定义按钮,我很确定 Apple 会抱怨。文档非常具体,我们不应该做任何与 Apple 图标相似的事情。

提示?

谢谢!

编辑:我想要 unselected=white 的原因是,在我设计的背景下,默认的灰色会使图标/文本很难看。

【问题讨论】:

    标签: ios ios7 uitabbaritem uiappearance


    【解决方案1】:

    在 iOS 10 中,UITabBar 公开了unselectedItemTintColor,您可以使用它来指定未选中项目的色调。请参阅docs 了解更多信息。

    这意味着当您配置​​标签栏时,您有以下色调选项:

    • tintColor 适用于选定的选项卡
    • unselectedItemTintColor 适用于所有未选中的标签

    【讨论】:

      【解决方案2】:

      如果您使用 Storyboard,您还可以同时设置 Image 为 Bar Item 和 Selected Image 为 Selected Bar Item 以在 tabBar 中获得不变的图像。

      【讨论】:

        【解决方案3】:

        您准备好使用一些私有 API 了吗?

        在填充标签栏后的某个时间执行此操作:

        for (UIView *v in self.tabBar.subviews)
          if ([NSStringFromClass(v.class) isEqual:@"UITabBarButton"])
            [v performSelector:@selector(_setUnselectedTintColor:)
                      withObject:[UIColor whiteColor]];
        

        适用于私有 API 的常见警告:Apple 不希望您这样做;如果它坏了,你可以保留两块;等等

        示例代码在 App Store 审查方面并不是特别隐秘。正如所介绍的那样,Apple 会抓住它。添加您最喜欢的隐身技术以适应。

        至少可在 iOS 7.0.1 和 7.1 上运行。

        【讨论】:

        • (我检查了,秘密 UITabBarButton 类的秘密 unselectedTintColor 属性不受外观代理支持。所以遗憾的是,你不能提前这样做一次并完成,你必须将其应用于实际填充的标签栏。)
        【解决方案4】:

        我成功地将自定义未选择的 UITabBarItem 图像设置为白色的唯一方法是使用以下(白色)图像并以特定的渲染模式加载它

        UIImage *tabImage = [[UIImage imageNamed:@"white_tab_item"] 
                              imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        self.tabItem.image = tabImage;
        

        “tabItem”是该类的 UITabBarItem Outlet

        归功于 Aaron Brager 的回答: UITabBarController unselected icon image tint

        【讨论】:

        • 这应该被标记为接受的答案。 @rgeorge 的私有 api 解决方案有效,但苹果不会接受该应用程序进入应用商店。
        【解决方案5】:

        此代码更改选择按钮标题颜色

        ////为ios7定义这些代码

          #define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion]   compare:v options:NSNumericSearch] == NSOrderedSame)
        
         #define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
        
         #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
        
         #define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
        
         #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
        
        
        
        
        
         if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")){
        
           NSLog(@"ios77777");
        
        
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont            fontWithName:@"HelveticaNeue-Bold" size:10.0f],
                                                                NSForegroundColorAttributeName :   [UIColor redColor]
        
        
        
         }
        
              forState:UIControlStateNormal];
        
            }
        

        【讨论】:

          【解决方案6】:

          应该是:

          [[UITabBar appearance] setTintColor:[UIColor whiteColor]]
          

          取自:iOS 自定义 UI 系列:TabBar & NavBar:http://bit.ly/1ahr9Ao

          【讨论】:

          • 确定你是指 iOS7 而不是 iOS6?
          猜你喜欢
          • 1970-01-01
          • 2015-06-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-03-18
          • 2015-10-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多