Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
UINavigationController custom navigation bar
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Create a new window and assign directly to provided iVar
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Implementation of new init method
MyCustomNavigationBar *navigationBar = [[MyCustomNavigationBar alloc] initWithFrame:CGRectZero];
UINavigationController *navigationController = [[UINavigationController alloc] initWithCustomNavigationBar:navigationBar];
[navigationBar release];
[self.window setRootViewController:navigationController];
[navigationController release];
[self.window makeKeyAndVisible];
return YES;
}
#import <UIKit/UIKit.h>
@interface UINavigationController (MTCustomNavigationBar)
- (id)initWithCustomNavigationBar:(UINavigationBar *)navigationBar;
@end
#import "UINavigationController-MTCustomNavigationBar.h"
#import <objc/runtime.h>
@implementation UINavigationController (MTCustomNavigationBar)
- (id)initWithCustomNavigationBar:(UINavigationBar *)navigationBar
{
self = [self initWithNibName:nil bundle:nil];
if (self)
{
// Init
[self setValue:navigationBar forKey:@"navigationBar"];
}
return self;
}
@end
@ihmunro

This comment has been minimized.

Show comment Hide comment
@ihmunro

ihmunro Feb 19, 2012

Does not work

ihmunro commented Feb 19, 2012

Does not work

@Nitewriter

This comment has been minimized.

Show comment Hide comment
@Nitewriter

Nitewriter Feb 21, 2012

This method is not ARC compatible. If you are using ARC you can simply replace the navigation bar using:
[navigationController setValue:navigationBar forKey:@"navigationBar"].

Owner

Nitewriter commented Feb 21, 2012

This method is not ARC compatible. If you are using ARC you can simply replace the navigation bar using:
[navigationController setValue:navigationBar forKey:@"navigationBar"].

@wenboqiu

This comment has been minimized.

Show comment Hide comment
@wenboqiu

wenboqiu Mar 11, 2012

Hi Nitewriter:

I test your code on ios5.1, the custom navigation bar appears, but the back button on the bar doesn't work, it doesn't call popViewControllerAnimated method when tap back button, however the back button will disappear...it is weird.

best regards

Hi Nitewriter:

I test your code on ios5.1, the custom navigation bar appears, but the back button on the bar doesn't work, it doesn't call popViewControllerAnimated method when tap back button, however the back button will disappear...it is weird.

best regards

@Nitewriter

This comment has been minimized.

Show comment Hide comment
@Nitewriter

Nitewriter Mar 12, 2012

iOS 5.x introduced changes to the UINavigationBar that cause issues when using this method of replacement. You should use the setValue:forKey: replacement I mentioned to ihmunro.

Owner

Nitewriter commented Mar 12, 2012

iOS 5.x introduced changes to the UINavigationBar that cause issues when using this method of replacement. You should use the setValue:forKey: replacement I mentioned to ihmunro.

@wenboqiu

This comment has been minimized.

Show comment Hide comment
@wenboqiu

wenboqiu Mar 13, 2012

thanks for your feedback

thanks for your feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment