Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

correct way of using UINavigationController's initWithNavigationBarClass:toolbarClass:

Tags:

ios

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?

like image 698
inforeqd Avatar asked Jan 31 '13 03:01

inforeqd


3 Answers

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.

enter image description hereenter image description here

like image 192
iDev Avatar answered Oct 19 '22 03:10

iDev


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.

like image 6
Benjamin Mayo Avatar answered Oct 19 '22 03:10

Benjamin Mayo


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;
}
like image 2
Dan Rosenstark Avatar answered Oct 19 '22 02:10

Dan Rosenstark