IOS 8选项卡栏项目背景颜色

上周我一直在努力寻找解决scheme,在尝试find或想到的所有可能的解决scheme之后,我一直没有运气。 我发现和尝试的每个解决scheme都没有工作,或者已经过时。

我有UITabBar放在UITabBarController内的UITabBarController 。 我想在select时更改UITabBarItem的背景颜色,当然当select的项目发生变化时它会变回。

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

编辑:这是我想要它做的一个视觉例子。

不同的背景颜色

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

  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类来制作您所需大小的普通彩色图像:

 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 } } 

你可以试试这个。 将其添加到AppDelegate.swift

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. UITabBar.appearance().translucent = false UITabBar.appearance().barTintColor = UIColor(rgba: "#12296f") UITabBar.appearance().tintColor = UIColor.whiteColor() return true } 

不要忘记包含这个库。 https://github.com/yeahdongcn/UIColor-Hex-Swift

Gwendle的启发,这是我解决它的方法:

 override func viewWillAppear(animated: Bool) { guard let tabBar = tabBarController?.tabBar else { return } tabBar.tintColor = UIColor.whiteColor() tabBar.selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.redColor(), size: CGSizeMake(tabBar.frame.width/5, tabBar.frame.height)) super.viewWillAppear(animated) } 

还使用了扩展名:

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

请记住,设置selectionIndicationImage它将保持为所有其他选项卡设置。 下面是一个如何将其删除的示例,方法是将其设置为在其余选项卡中的每个其他视图控制器中为零:

 override func viewWillAppear(animated: Bool) { tabBarController?.tabBar.tintColor = UIColor.redColor() tabBarController?.tabBar.selectionIndicatorImage = nil super.viewWillAppear(animated) } 

用Swift 2实现

你尝试过吗?

在故事板中的视图控制器中select标签栏图标图像。

在xcode的右侧面板中查看身份和types(最左边)选项卡(它看起来像一张纸)。

寻找全球色调设置。