IOS 8标签栏项目背景颜色 [英] IOS 8 Tab Bar Item Background Colour

查看:88
本文介绍了IOS 8标签栏项目背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上周我一直试图找到解决方案,在尝试了我能找到或想到的每一个可能的解决方案后,我没有运气。我发现并尝试过的每个解决方案都没有工作,或者已经过时。

I've been trying to find the solution to this for the last week, and I have had no luck after trying every possible solution I could find or think of. Every solution I found and have attempted has either not worked, or been outdated.

我有5 UITabBarItem 's在 UITabBar 中放置在 UITabBarController 中。我想在选择它时更改 UITabBarItem 的背景颜色,当然还要在所选项目更改时更改它。

I have 5 UITabBarItem's in a UITabBar placed within UITabBarController. I want to change the background color of the UITabBarItem when it is selected, and of course have it change back when the selected item changes.

我在Xcode 6.3.1中使用Swift和iOS SDK 8.3。如果您只能在Objective-C中回答也没问题,那么任何答案都会有所帮助!提前谢谢大家,我真的很感激!

I am using Swift, and iOS SDK 8.3 in Xcode 6.3.1. If you can only answer in Objective-C that is fine too, any answer will help! Thank you all in advance, I really appreciate it!

编辑:这是我希望它做的一个直观的例子。

Here is a visual example of what I would want it to do.

不同背景颜色

推荐答案

在你的tabBarController中,你可以设置默认的UITabBar tintColor,barTintColor,selectionIndicatorImage(这里有点作弊)和图像的renderingMode,见下面的评论:

In your tabBarController, you can set the default UITabBar tintColor, barTintColor, selectionIndicatorImage (cheating a bit here) and renderingMode of the images, see comments below:

    class MyTabBarController: UITabBarController, UINavigationControllerDelegate {
      ...
      override func viewDidLoad() {
        ...
        // Sets the default color of the icon of the selected UITabBarItem and Title
        UITabBar.appearance().tintColor = UIColor.redColor()

        // Sets the default color of the background of the UITabBar
        UITabBar.appearance().barTintColor = UIColor.blackColor()

        // Sets the background color of the selected UITabBarItem (using and plain colored UIImage with the width = 1/5 of the tabBar (if you have 5 items) and the height of the tabBar)
        UITabBar.appearance().selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.blueColor(), size: CGSizeMake(tabBar.frame.width/5, tabBar.frame.height))

        // Uses the original colors for your images, so they aren't not rendered as grey automatically.
        for item in self.tabBar.items as! [UITabBarItem] {
          if let image = item.image {
            item.image = image.imageWithRenderingMode(.AlwaysOriginal)
          }
        }
      }
      ...
    }

你会想要扩展UIImage类来制作平面你需要大小的彩色图像:

And you will want to extend the UIImage class to make the plain colored image with the size you need:

extension UIImage {
  func makeImageWithColorAndSize(color: UIColor, size: CGSize) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(size, false, 0)
    color.setFill()
    UIRectFill(CGRectMake(0, 0, size.width, size.height))
    var image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
  }
}

这篇关于IOS 8标签栏项目背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆