【问题标题】:How can I change a color of image and label on UITabBar on iOS 7.1?如何在 iOS 7.1 中更改 TabBar 上的图像和标签颜色?
【发布时间】:2014-03-28 20:12:07
【问题描述】:

如何在 iOS 7.1 上更改 UITabBar 上的图像和标签颜色?在 iOS 7 上,我可以通过 Tint 属性实现它。但在 iOS 7.1 上它不起作用。

【问题讨论】:

    标签: colors uitabbar uitabbaritem ios7.1


    【解决方案1】:

    它在 iOS 7 和 iOS 7.1 中的工作方式相同!

    在 AppDelegate 中:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Add this if you only want to change Selected Image color 
        // and/or selected image text
        [[UITabBar appearance] setTintColor:[UIColor redColor]];
    
        // Add this code to change StateNormal text Color,
        [UITabBarItem.appearance setTitleTextAttributes:
        @{NSForegroundColorAttributeName : [UIColor greenColor]} 
        forState:UIControlStateNormal];
    
        // then if StateSelected should be different, you should add this code
        [UITabBarItem.appearance setTitleTextAttributes:
        @{NSForegroundColorAttributeName : [UIColor purpleColor]} 
        forState:UIControlStateSelected];
    
        return YES;
    }
    

    在每个 ViewController 中:(如果要更改未选中的图像颜色)

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // changing the unselected image color, you should change the selected image 
        // color if you want them to be different
        self.tabBarItem.selectedImage = [[UIImage imageNamed:@"yourImage_selectedImage"]
        imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
        self.tabBarItem.image = [[UIImage imageNamed:@"yourImage_image"] 
        imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    

    这段代码的线索是'UIImageRenderingModeAlwaysOriginal':

    Apple 文档的渲染模式:

    UIImageRenderingModeAutomatic,          // Use the default rendering mode for the context where the image is used    
    UIImageRenderingModeAlwaysOriginal,     // Always draw the original image, without treating it as a template
    UIImageRenderingModeAlwaysTemplate,     // Always draw the image as a template image, ignoring its color information
    

    【讨论】:

      【解决方案2】:

      UITabBarItem 从未拥有tint 属性。 没有内置类曾经有过tint 属性。这里没有任何变化。

      在 iOS 7 和 7.1 中,UITabBar 具有 tintColor 属性,因为它是 UIView。

      【讨论】:

        【解决方案3】:

        这会在选择时更改图像和标签的色调。

        - (void)viewDidLoad
        {
            [super viewDidLoad];
            [[UITabBar appearance] setTintColor:[UIColor redColor]];
        }
        

        【讨论】:

          猜你喜欢
          • 2019-11-28
          • 2013-06-11
          • 2015-09-15
          • 1970-01-01
          • 1970-01-01
          • 2014-09-12
          • 1970-01-01
          • 2013-09-18
          • 1970-01-01
          相关资源
          最近更新 更多