2016-06-09 64 views
0

我无法通过按下uibutton更新uibartint颜色,它不起作用。这是我的层次结构:我如何实时更新UITabBarController外观?

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]; 

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

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

在我的情况不工作

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

没有我的情况下

setNeedsStatusBarAppearanceUpdate 

工作在我的情况下不能正常工作。

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

回答

0

试试下面的代码

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

[self.tabBarController setTintColor:blu]; 
+0

它的工作原理,但以这种方式: [self.tabBarController.tabBar setTintColor:blu]; – user31929

0

一旦通过将它放在主线程类似尝试,

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]; 
}); 
+0

刚刚试过,在childviewcontrollerē的appdelegate,不工作 – user31929