Search code examples
iphoneiosuinavigationcontrollerpushviewcontroller

How to create a UINavigationController that pushes from the top of the screen?


I'm creating an iPhone app with a standard "Master-Detail" navigation, where you choose an item from a UITableView, and see the Detail view for the item in a ViewController that pushes onto the screen.

However: unlike a traditional UINavigationController, which pushes the detail screen from the right-hand side of the window, I'd like to have my detail screen come down from the top of the screen.

What would I need to accomplish this? Do I have to subclass UINavigationController? (I've heard that's not a good idea). What class(es) should I consider subclassing?


Solution

  • Subclass UINavigationController and override pushViewController:animated:.
    Don't immediately call super and do you custom animation stuff like this:

    nextViewController.view.transform = CGAffineTransformMakeTranslation(0, -nextViewController.view.frame.size.height);
    
    void(^animationBlock)(void) = ^{
        [super pushViewController:nextViewController animated:NO];
        nextViewController.view.transform = CGAffineTransformIdentity;
    };
    
    if (animated) {
        [UIView animateWithDuration:0.3f animations:animationBlock];
    }
    else{
        animationBlock();
    }
    

    Update:

    You actually don't need to subclass UINavigationController. You also can do the animation stuff above every time you push a VC to a regular UINavigationController.