Skip to content

Instantly share code, notes, and snippets.

@marshluca
Forked from Nitewriter/MyAppDelegate.m
Created October 24, 2011 04:03
Show Gist options
  • Save marshluca/1308357 to your computer and use it in GitHub Desktop.
Save marshluca/1308357 to your computer and use it in GitHub Desktop.
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
// Verify subclasses parent class is UINavigationBar
if (navigationBar != nil && [navigationBar isKindOfClass:[UINavigationBar class]])
{
UINavigationBar *navigationBar__ = nil;
// Get the unexposed ivar for navigation bar replacement
Ivar ivar_navigationBar__ = object_getInstanceVariable(self, "_navigationBar", (void **)&navigationBar__);
// Clean up memory if a navigation bar has already been assigned
if (navigationBar__ != nil)
[navigationBar__ release];
// Assign the custom navigation bar to the retrieved ivar
navigationBar__ = [navigationBar retain];
object_setIvar(self, ivar_navigationBar__, navigationBar__);
}
}
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment