2016-06-09 37 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