IOS 8 标签栏项目背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30041127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:53:11  来源:igfitidea点击:

IOS 8 Tab Bar Item Background Colour

iosswiftios8uitabbaruitabbaritem

提问by user2985289

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 UITabBarplaced within UITabBarController. I want to change the background color of the UITabBarItemwhen it is selected, and of course have it change back when the selected item changes.

我有 5UITabBarItemUITabBar放置在UITabBarController. 我想改变UITabBarItem它被选中时的背景颜色,当然当被选中的项目改变时它会变回来。

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!

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

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

编辑:这是我希望它做什么的视觉示例。

Different Background Color

不同的背景颜色

回答by Gwendle

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(这里有点作弊)和图像的渲染模式,见下面的评论:

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

回答by Mohammad Nurdin

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

回答by Andrej

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 selectionIndicationImageit remains set for all your other tabs. Here's an example how to remove it, by setting it to nil in every other view controller in the remaining tabs:

请记住,在您设置之后,selectionIndicationImage它仍然为所有其他选项卡设置。这是一个如何删除它的示例,方法是在其余选项卡的每个其他视图控制器中将其设置为 nil:

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

Implemented using Swift 2.

使用 Swift 2 实现。

回答by BSK-Team

You can call this function from each controller passing self.tabBarControllerand 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
            }
        }
    }

回答by Garret

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 右侧面板上的 Identity and Type(最左侧)选项卡(看起来像一张纸)。

Look for the global tint setting.

寻找全局色调设置。