简体   繁体   English

Swift 2.2如何更改标签栏控制器的背景颜色

[英]Swift 2.2 How to change background color of tab bar controller

I've been trying to change the background of the tab bar, and have read many posts here in ObjC, many of them quite old. 我一直在尝试更改标签栏的背景,并在ObjC中阅读了很多帖子,其中很多都很老了。 But I didn't find any for Swift, which is what I'm using. 但我没有找到任何Swift,这是我正在使用的。

I finally figured out how to do it from code, so the tab bar colors can be changed with each theme color change. 我终于想出了如何从代码中做到这一点,因此标签栏颜色可以随着每个主题颜色的变化而改变。 Here are the references that I used: 以下是我使用的参考资料:

  override func viewWillAppear(animated: Bool) {
    // set tab bar background color, including the More tab
    self.tabBarController?.tabBar.backgroundColor = UIColor.blueColor()
  }

I put this code in the view controller for the first tab that appears when the app starts up, so that it gets run "first." 我将此代码放在视图控制器中,用于应用程序启动时显示的第一个选项卡,以便它“首先”运行。 It works fine as far as I can tell, even when I have 8 tabs and use the More... tab. 据我所知,即使我有8个标签并使用更多...标签,它也能正常工作。

And I bound the same code to buttons in various tabs, so I can change the tab bar color from anywhere in my code. 我将相同的代码绑定到各个选项卡中的按钮,因此我可以在代码中的任何位置更改选项卡栏颜色。

But I confess that as a newbie, I'm not sure that's the best location to change the tab bar color. 但我承认,作为一个新手,我不确定这是改变标签栏颜色的最佳位置。 If this is the wrong place, please correct me. 如果这是错误的地方,请纠正我。

I post this code here since it would have saved me a few hours, and I hope that it can save someone else some (swift) time. 我在这里发布这段代码,因为它可以节省我几个小时,我希望它可以节省一些(快速)时间。

This is a fine way to change the color of a UITabBar . 这是改变UITabBar颜色的好方法。 If you want to avoid setting the color in every viewController that is embedded inside of your UITabBarController , you could also create a subclass of UITabBarController and set it there. 如果你想避免在嵌入UITabBarController内部的每个viewController中设置颜色,你也可以创建一个UITabBarController的子类并在那里设置它。 This way no matter what page comes up first, the color will be set. 这种方式无论首先出现什么页面,都会设置颜色。

To create a subclass of UITabBarController , just go to file > new > file > cocoa touch class...Then setup your file like in this photo 要创建UITabBarController的子类,只需转到file> new> file> cocoa touch class ...然后像在这张照片中一样设置文件

添加文件

Now in your storyboard, set the custom class on your tabBarController 现在在故事板中,在tabBarController上设置自定义类

故事板

Finally, in your file you created MyTabBarController (or whatever you called it): 最后,在你的文件中创建了MyTabBarController (或者你称之为的任何东西):

class MyTabBarController: UITabBarController {

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    self.tabBar.barTintColor = .blueColor()
}
...

Xcode 9 Xcode 9

Storyboard 故事板

It possible to set background color directly in view controller (currently in TabBarViewController), for this you need to define Key Path . 可以直接在视图控制器中设置背景颜色(当前在TabBarViewController中),为此您需要定义键路径 Don't forget to remove background color on nested view, otherwise it will overlap superview's background color. 不要忘记在嵌套视图上删除背景颜色,否则它将与superview的背景颜色重叠。

在此输入图像描述

You can change like this 你可以这样改变

tabBarController.tabBar.barTintColor = [UIColor blackColor]; tabBarController.tabBar.barTintColor = [UIColor blackColor];

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

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