2014-01-10 76 views
0

在iOS7,我們可以通過如何在沒有iOS7動畫的情況下更改barTintColor?

self.navigationController.navigationBar.barTintColor = [UIColor redColor]; 

改變導航欄的顏色,但這種方法有改變barTintColor時的動畫漸變,因此,沒有anynone知道如何防止這種情況的動畫,並立即更改顏色?

更具體地說,我寫了一個測試程序,其窗口的根控制器是navigationController。在navigationController中,有一個帶3個按鈕的視圖控制器。 3個按鈕都結合以下動作:

- (void)onClick:(id)sender 
{ 
    UIColor *color = nil; 
    if (sender == self.redButton) 
    { 
     color = [UIColor redColor]; 
    } 
    else if (sender == self.blueButton) 
    { 
     color = [UIColor blueColor]; 
    } 
    else if (sender == self.blackButton) 
    { 
     color = [UIColor blackColor]; 
    } 

    self.navigationController.navigationBar.barTintColor = color 

// [UIView animateWithDuration:0 animations:^{ 
//  self.navigationController.navigationBar.barTintColor = color; 
// }]; 

// [CATransaction begin]; 
// [CATransaction setDisableActions:YES]; 
// self.navigationController.navigationBar.barTintColor = color; 
// [CATransaction commit]; 

} 

兩者改變動畫的持續時間爲0,或使用[CATransaction setDisableActions:YES]不起作用,動畫仍然存在。

希望有人能幫忙,謝謝!

回答

-1

嘗試用

[UIView animateWithDuration:0 animations:^{ 
     self.navigationController.navigationBar.barTintColor = [UIColor redColor]; 
}]; 
+0

你運行該代碼後?動畫仍然存在,動畫持續時間不會改變。 – tctony

1

你需要禁用隱式動畫。您可以這樣做:

[CATransaction begin]; 
[CATransaction setDisableActions: YES]; 

self.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithRed:125.0/255.0 green:90.0/255.0 blue:146.0/255.0 alpha:1]; 

[CATransaction commit]; 

此技術適用於任何隱式動畫。隱式動畫是當您更改動畫屬性時由iOS爲您創建的動畫。在這裏看到更多的信息:

https://developer.apple.com/library/ios/documentation/cocoa/conceptual/coreanimation_guide/CreatingBasicAnimations/CreatingBasicAnimations.html

+0

對不起,您是否在navigationBar.barTintColor上嘗試過它?它只是不起作用。 – tctony

+0

在UITabBarController.tabBar.barTintColor上沒有適用於我。 – bdmontz

3

嘗試重新設置之前,barTintColor設置爲零。

self.navigationController.navigationBar.barTintColor = nil; 
self.navigationController.navigationBar.barTintColor = color; 

我有類似的問題,它修復了我。希望能幫助到你。

+0

這不適合我。 – Neeku

+0

這是爲我工作的唯一解決方案。 我有我的導航欄,從一種顏色到另一種顏色基於手勢淡出,這完美的作品。謝謝 –

1

translucent財產上的UINavigationBarNO,然後重置它吧色調改變

self.navigationBar.translucent = NO; 
self.navigationBar.barTintColor = [UIColor magentaColor]; 
self.navigationBar.translucent = YES; 
+0

這是這個線程中唯一適用於我的方法。謝謝。 – Ender2050

相關問題