iOS same UINavigationBar for all UINavigationControllers

i have a tabbed application with 4 navControllers, which all should use the SAME UINavigationBar (same color, same buttons). now i only want to create the navbar once!

my first approach was to subclass UINavigationController, change the bar color and buttons, and use it for the navControllers in my AppDelegate, but the buttons didn't show up and i found out that the documentation says that you're not supposed to subclass UINavigationController...

can you help me out? i coulnd't find a solution to this anywhere...

Answers


If you're targeting iOS 5 only you may use the appearance proxy, which lets you customize UI elements at one point for the whole application.

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

If you need to do this on older iOS' then there is no really nice solution to do this. There is an approach which uses method swizzling, described here.

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

But this does not work on iOS 5 anymore. Best approach is to use the appearance proxy for iOS 5 and a workaround like the method swizzling for older iOSes.

Edit:

Here's some code I found to use the appearance proxy if it's available and use method swizzling if it's not.

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);
}

The drawRectCustomBackground method is implemented in a category on UINavigationBar.


Start your main navigation controller with 4 arrays inside and each array has one NavigationController inside for each tab of the NavigationBar. I think it will work.

Hope it helps,

Mário


Need Your Help

Android alarm setting with specific date

android calendar alarmmanager notificationmanager

I wan to set alarm with notification at specific date.

Inserts wrong value to MySQL table

php javascript html mysql insert

I have dynamic list from MySQL table on my php page so that HTML part below is 10-20 times per page. If I click that submit-button it does insert stuff to my database but player_id is always the id...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.