IOS 8标签栏项目背景颜色

use*_*289 18 uitabbaritem uitabbar ios swift ios8

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

我有5 UITabBarItemUITabBar放在里面UITabBarController.我想要更改UITabBarItem选择它时的背景颜色,当然还要在所选项目更改时更改.

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

编辑:这是我希望它做的一个视觉示例.

不同的背景颜色

Gwe*_*dle 64

在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)
          }
        }
      }
      ...
    }
Run Code Online (Sandbox Code Playgroud)

并且您将需要扩展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
  }
}
Run Code Online (Sandbox Code Playgroud)


Moh*_*din 20

你可以尝试这个.添加此内容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
    }
Run Code Online (Sandbox Code Playgroud)

别忘了包含这个库.https://github.com/yeahdongcn/UIColor-Hex-Swift


Gar*_*ret 0

你试过这个吗?

在故事板的视图控制器中选择选项卡栏图标图像。

查看 xcode 右侧面板上的 Identity and Type(最左侧)选项卡(它看起来像一张纸)。

寻找全局色调设置。