应用加载后更改 TabBar 颜色

Change TabBar Color after app has loaded

如果我将代码放入 AppDelegate 的 DidFinishLaunchingWithOptions 函数中,我可以轻松更改选项卡栏的颜色,但如果我在 ViewController 中调用具有相同代码的函数,则它不会改变。例如,我怎样才能在按下视图控制器中的按钮后改变它?

这是 DidFinishLaunchingWithOptions 函数中的代码:

 UITabBar.appearance().tintColor = UIColor.white
 UITabBar.appearance().barTintColor = UIColor.black

使用 appearance 代理为之后创建的任何新控件设置属性。它不会更改任何现有的相关控件。

如果您希望设置特定的现有实例的属性,请直接在控件上设置属性,而无需使用 appearance 代理。

someTabBar.tintColor = UIColor.purple
someTabBar.barTintColor = UIColor.orange