From the apple docs I understand that a UiNavigationController can be instantiated with another Uinavigationbar using initWithNavigationBarClass:toolbarClass: method. How does one correctly do this via a custom UiNavigationBar subclass and IB?
You can use it like this to initialize the navigation controller,
UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[CustomNavigationBar class] toolbarClass:nil];
Here CustomNavigationBar
is the custom class created by subclassing UINavigationBar
. You can set the viewcontrollers by using the setViewControllers
property of UINavigationController.
If you want to do this in IB, try this. Select navigation bar from objects and in the identity inspector, select the custom class for navigationbar.
In Interface Builder, you click on the navigation bar inside the navigation controller. Inspect it on the right panel, and change the custom class from UINavigationBar
to your custom subclass.
In code, make sure you have imported your header file for the navigation bar subclass and write something similar to the following.
// 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];
The code above informs UIKit to create a UINavigationController
with navigation bars of subclass MyCustomNavigationBar
. Then, it sets the root view controller to the object stored in the variable viewController
.
Just mashing up Benjamin Mayo's answer here for your general subclass
- (UINavigationController *)initWithRootViewController:(UIViewController *)rootViewController navigationBarClass:(Class)navigationBarClass {
self = [super initWithNavigationBarClass:navigationBarClass toolbarClass:UIToolbar.class];
if (self) {
self.viewControllers = @[rootViewController];
}
return self;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With