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

Handling ViewExpiredException on login page using a custom ViewHandler

java jsf-2 facelets viewexpiredexception

In my web app, when the session expires and a user initiates a request (full page or AJAX), the user is redirected to the login page with a message that the session expired. The problem is, when the

Jquery - Code that dynamically scrolls a div to the bottom not working

jquery scroll

Most of the solutions provided in this site use some variants of this code to scroll down to the bottom of a div

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.