簡體   English   中英

iOS對所有UINavigationControllers都使用相同的UINavigationBar

[英]iOS same UINavigationBar for all UINavigationControllers

我有一個帶有4個navControllers的選項卡式應用程序,它們都應使用SAME UINavigationBar(相同的顏色,相同的按鈕)。 現在我只想創建一次導航欄!

我的第一種方法是將UINavigationController子類化,更改條形顏色和按鈕,並將其用於我的AppDelegate中的navControllers,但是按鈕沒有出現,我發現文檔說您不應該將UINavigationController子類化...

你能幫我嗎? 我在任何地方都找不到解決方案...

如果僅以iOS 5為目標,則可以使用外觀代理,該代理可讓您一次為整個應用程序自定義UI元素。

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAppearance_Protocol/Reference/Reference.html

如果您需要在舊版iOS上執行此操作,則沒有真正好的解決方案。 這里有一種使用方法混淆的方法。

http://samsoff.es/posts/customize-uikit-with-method-swizzling

但這在iOS 5上不再有效。 最好的方法是使用iOS 5的外觀代理,並使用一種變通方法,例如老版本iOS的方法。

編輯:

這是一些我發現使用外觀代理的代碼(如果可用),如果不使用,則使用swizzling方法。

if ([[UINavigationBar class] respondsToSelector:@selector(appearance)]) {
    // iOS >= 5.0 -> Use Appearance API
    [[UINavigationBar appearance] setTintColor:[UIColor redColor]];
}
else {
    // iOS < 5.0 -> Use Method Swizzling
    Method drawRectCustomBackground = class_getInstanceMethod([UINavigationBar class], @selector(drawRectCustomBackground:));
    Method drawRect = class_getInstanceMethod([UINavigationBar class], @selector(drawRect:));
    method_exchangeImplementations(drawRect, drawRectCustomBackground);
}

UINavigationBar的類別中實現了drawRectCustomBackground方法。

啟動內部4個數組的主導航控制器,每個數組內部每個NavigationBar選項卡都有一個NavigationController。 我認為它將起作用。

希望能幫助到你,

馬里奧

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM