Sunday, June 9, 2013

UINavigationController initWithNavigationBarClass example in Objective C (iOS).


UINavigationController initWithNavigationBarClass

Initializes and returns a newly created navigation controller that uses your custom bar subclasses.

- (instancetype)initWithNavigationBarClass:(Class)navigationBarClass toolbarClass:(Class)toolbarClass

Parameters
navigationBarClass
Specify the custom UINavigationBar subclass you want to use, or specify nil to use the standard UINavigationBar class.
toolbarClass
Specify the custom UIToolbar subclass you want to use, or specify nil to use the standard UIToolbar class.

Return Value of [UINavigationController initWithNavigationBarClass]
The initialized navigation controller object or nil if there was a problem initializing the object.

Discussion of [UINavigationController initWithNavigationBarClass]
Use this initialization method if you want to use custom navigation bar or toolbar subclasses with the navigation controller. If you use this method, you are responsible for adding a root view controller before presenting the navigation controller onscreen.

UINavigationController initWithNavigationBarClass example.
// This code assumes `MyCustomNavigationBar` is the name of your custom subclass, and that `viewController` is a UIViewController object created earlier.

// To create the containing navigation controller
UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[MyCustomNavigationBar class] toolbarClass:[UIToolbar class]];

// To set the root view controller in the navigation controller
navigationController.viewControllers = @[viewController];

Example of [UINavigationController initWithNavigationBarClass].
Like this:

UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[MyNavigationBar class] toolbarClass:[UIToolbar class]];

[navigationController setViewControllers:@[yourRootViewController] animated:NO];

UINavigationController initWithNavigationBarClass example.
e.g.

@interface MyBar : UINavigationBar
@end

@implementation MyBar
.... //like any UIView
@end
UINavigationController *navi = [[UINavigationController alloc] initWithNavigationBarClass:[MyBar class] toolbarClass:nil];
instead of initWithRootViewController

Sample

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    self.mainViewController = [[FDMainViewController alloc] initWithNibName:@"FDMainViewController_iPhone" bundle:nil];
} else {
    self.mainViewController = [[FDMainViewController alloc] initWithNibName:@"FDMainViewController_iPad" bundle:nil];
}

UINavigationController *navi = [[UINavigationController alloc] initWithNavigationBarClass:[UINavigationBar class] toolbarClass:nil];
navi.viewControllers = @[self.mainViewController];
self.window.rootViewController = navi;
[self.window makeKeyAndVisible];
return YES;
}

End of UINavigationController initWithNavigationBarClass example article.