我如何实时更新 UITabBarController 外观?

How i can update UITabBarController Appearance in real time?

我无法通过按 UIButton 来更新 UIBarTintColor,它不起作用。这是我的层次结构:

AppDelegate > HomeViewController (that holds my tabBarController) > ChildViewController

我想通过按下 ChildViewController 中的按钮实时更改 UiTabbarController 颜色。这是我使用的代码:

UIColor* blu = [UIColor colorWithRed: 0.0/255 green: 161.0/255 blue: 223.0/255 alpha: 1];
[[UIView appearance] setTintColor:blu];
[[UITabBar appearance] setTintColor:blu];
[[UISlider appearance] setTintColor:blu];
[[UINavigationBar appearance] setTintColor:blu];

它不会实时改变我的标签栏的颜色,只有当我在标签栏的初始化过程中使用它时它才会改变颜色。我已经搜索并尝试了不同的解决方案:

[self.tabBarController.tabBar setHidden:YES];
[self.tabBarController.tabBar setHidden:NO];

不适用于我的情况

CALayer *layer2 =  self.tabBarController.view.layer;
[layer2 setNeedsDisplay];
[layer2 displayIfNeeded];

不适用于我的情况

setNeedsStatusBarAppearanceUpdate

不适用于我的情况。

我还尝试在 appDelegate 中创建一个方法来包含所有这些尝试,我必须从 ChildViewController 中尝试这个方法,而不是直接调用代码。结果是一样的。 我该如何刷新 uitabbar 并通过按下按钮更改颜色?

一旦尝试将它放在主线程上,

dispatch_async(dispatch_get_main_queue(), ^{
    UIColor* blu = [UIColor colorWithRed: 0.0/255 green: 161.0/255 blue: 223.0/255 alpha: 1];
    [[UIView appearance] setTintColor:blu];
    [[UITabBar appearance] setTintColor:blu];
    [[UISlider appearance] setTintColor:blu];
    [[UINavigationBar appearance] setTintColor:blu];
});

试试下面的代码

UIColor* blu = [UIColor colorWithRed: 0.0/255 green: 161.0/255 blue: 223.0/255 alpha: 1];

[self.tabBarController.tabBar setTintColor:blu];