简体   繁体   English

IOS 8标签栏项目背景颜色

[英]IOS 8 Tab Bar Item Background Colour

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. 我发现并尝试过的每一个解决方案要么没有工作,要么已经过时了。

I have 5 UITabBarItem 's in a UITabBar placed within UITabBarController . 我有5 UITabBarItem的在一个UITabBar置于内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. 我想在选择UITabBarItem时更改它的背景颜色,当然,当所选项目发生变化时,它会更改回来。

I am using Swift, and iOS SDK 8.3 in Xcode 6.3.1. 我在Xcode 6.3.1中使用Swift和iOS SDK 8.3。 If you can only answer in Objective-C that is fine too, any answer will help! 如果您只能在Objective-C中回答也没问题,那么任何答案都会有所帮助! Thank you all in advance, I really appreciate it! 提前谢谢大家,我真的很感激!

EDIT: Here is a visual example of what I would want it to do. 编辑:这是我希望它做的一个视觉示例。

Different Background Color 不同的背景颜色

In your tabBarController, you can set the default UITabBar tintColor, barTintColor, selectionIndicatorImage (cheating a bit here) and renderingMode of the images, see comments below: 在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)
          }
        }
      }
      ...
    }

And you will want to extend the UIImage class to make the plain colored image with the size you need: 并且您将需要扩展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
  }
}

You can try this one. 你可以尝试这个。 Add this in AppDelegate.swift . 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
    }

Don't forget to include this library. 别忘了包含这个库。 https://github.com/yeahdongcn/UIColor-Hex-Swift https://github.com/yeahdongcn/UIColor-Hex-Swift

Inspired by Gwendle, this is how I've solved it: 灵感来自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)
}

And also used the extension: 并且还使用了扩展名:

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
}

Keep in mind that after you set the selectionIndicationImage it remains set for all your other tabs. 请记住,在设置selectionIndicationImage它将保留为所有其他选项卡设置。 Here's an example how to remove it, by setting it to nil in every other view controller in the remaining tabs: 这是一个如何删除它的示例,通过在其余选项卡中的每个其他视图控制器中将其设置为nil:

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

Implemented using Swift 2. 使用Swift 2实现。

Have you tried this? 你试过这个吗?

Select the tab bar icon image in your view controller in storyboard. 在故事板中的视图控制器中选择标签栏图标图像。

Look in the Identity and Type (far left) tab (it looks like a piece of paper) on the right panel of xcode. 在xcode的右侧面板中查看“身份和类型”(最左侧)选项卡(看起来像一张纸)。

Look for the global tint setting. 寻找全局色调设置。

You can call this function from each controller passing self.tabBarController and each color you want. 您可以从每个控制器调用此函数,传递self.tabBarController和您想要的每种颜色。

Function : 功能:

static func customTabBar(controller: UIViewController?, backgroundColor: String, unselectedColor: String, selectedColor: String) {
        if let tabBarController = controller as? UITabBarController {
            tabBarController.tabBar.barTintColor = UIColor(hex: backgroundColor)
            tabBarController.tabBar.tintColor = UIColor(hex: selectedColor)
            tabBarController.tabBar.isTranslucent = false
            tabBarController.tabBar.selectedItem?.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor(hex: selectedColor)], for: UIControl.State.selected)
            if #available(iOS 10.0, *) {
                tabBarController.tabBar.unselectedItemTintColor = UIColor(hex: unselectedColor)
            } else {
                // Fallback on earlier versions
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM