2012-08-07 26 views
4

我有一個要求,設置導航欄自定義顏色,這下面的代碼將做到這一點:UINavigationBar的:出現作品,但不是UINavigationBar的:appearanceWhenContained在

[[UINavigationBar appearance] 
      setBackgroundImage:navigationBarTileImage forBarMetrics:UIBarMetricsDefault]; 

但是我的應用程序調用系統MFMailComposeViewController和MFMessageComposeViewController我想導航欄是爲那些欣賞到默認的顏色,所以我這樣做:

[[UINavigationBar appearanceWhenContainedIn: [MyViewControllerBase class], [MyViewController1 class], [MyViewController2 class], nil] 
    setBackgroundImage:navigationBarTileImage forBarMetrics:UIBarMetricsDefault]; 

但是現在導航欄不再有我的默認顏色。 爲什麼外觀無法正常工作?

+0

詳細回答了截圖。 http://stackoverflow.com/a/19105760/1178039 – Uladzimir 2013-10-22 06:24:35

回答

19

appearanceWhenContainedIn:的參數代表一個視圖(和/或視圖控制器)的包含層次結構,而不是可能的容器列表。 (誠​​然,the docs不明確這一點,見the video from WWDC 2011)。因此,

[UINavigationBar appearanceWhenContainedIn:[NSArray arrayWithObjects:[MyViewControllerBase class], [MyViewController1 class], [MyViewController2 class], nil]] 

爲您提供包含在MyViewControllerBase內的UINavigationBar的外觀代理,這又是一個MyViewController1一個MyViewController2內內。我猜這不是你擁有的收容層次。

相反,看看包含導航欄的視圖控制器。這可能是一個通用的UINavigationController ......但你不能只是做

[UINavigationBar apperanceWhenContainedIn:[NSArray arrayWithObject:[UINavigationController class]]] 

,因爲這樣你會得到郵件/消息控制器了。但遺憾的是,儘管你在可以在郵件/消息視圖控制器中獲得UINavigationBar的外觀代理,但是沒有辦法告訴它撤消在更通用的級別上進行的外觀更改。

它看起來像這種情況下通常的解決方案是讓自己的UINavigationController子類,並將其用於你想要皮膚的部分你的用戶界面。子類可以是空的 - 只存在用於標識appearanceWhenContainedIn:的部分UI。 (同時,像MFMailComposeViewController繼續使用默認的外觀,因爲他們仍然使用通用UINavigationController。)

+0

我會看看那個視頻,看起來像是2012年的相關視頻。在我知道MF控制器將被推動之前,會使用外觀:並將其設置回默認色調,然後在彈出後將其設置回我的淺色工作?我會嘗試一個實驗,看看,我應該猜測。 – Gruntcakes 2012-08-08 17:35:37

+0

它會工作嗎?嘗試一下!然而,這是對正常API模式的迴避 - 即使它現在起作用,蘋果公司也承諾,如果你以正確的方式做到這一點,那麼未來的操作系統版本將繼續發揮作用。 – rickster 2012-08-13 05:17:23

+0

我試過了,但沒有奏效 – Gruntcakes 2012-08-13 15:08:01

0

事實上,像@rickster表示,appearanceWhenContainedIn:方法來定製出場包含在一個實例的類的實例容器類或層次結構中的實例。

不是在每種情況下都有一組您想要自定義的包含類,但不同的容器。能夠自定義多個組件的解決方案只需創建需要定製的類的數組,然後迭代!像這樣:

NSArray *navigationClass = [NSArray arrayWithObjects:[BSNavigationController class], [DZFormNavigationController class], nil]; 

for (Class class in navigationClass) 
{ 
    //// Customize all the UINavigationBar background image tilling 
    [[UINavigationBar appearanceWhenContainedIn:class, nil] setBackgroundImage:[UIImage imageNamed:@"yourImage"] forBarMetrics:UIBarMetricsDefault]; 
    [[UINavigationBar appearanceWhenContainedIn:class, nil] setTintColor:[UIColor blackColor]]; 

    // Title Text Attributes 
    NSDictionary *titleAttributes = [NSDictionary dictionaryWithObjectsAndKeys: 
            [UIColor whiteColor], UITextAttributeTextColor, 
            [UIColor darkGrayColor], UITextAttributeTextShadowColor, 
            [UIFont boldSystemFontOfSize:20.0], UITextAttributeFont, 
            [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,nil]; 

    //// Customize all the UINavigationBar title attributes 
    [[UINavigationBar appearanceWhenContainedIn:class, nil] setTitleTextAttributes:titleAttributes]; 
}