Determine form-sheet or navigation controller presentation

I recently ran into a situation where I needed to determine if a view controller was in a form sheet, or pushed onto a navigation stack.

A pattern I use for things like Settings in a universal app is to push the Settings view controller onto the stack, when on the iPhone, and wrap it in a UINavigationController and present that as a form sheet on the iPad. This works well, but I’ve been working on making my apps adaptive, and my old method of deciding which to use based on the UIUserInterfaceIdiom is not going to cut it in an adaptive world.

One odd thing about UIPresentationController is it doesn’t seem to have a way to determine how a view controller is currently presented. You can find out how the view controller would like to have been presented, but this doesn’t always match what it actually did.

In the end I kind of cheated, but I feel like it’s a good cheat so I’m sharing it.  It boils down to this:

    if (self.navigationController.viewControllers.count > 1) {

        // No “Done” button when in a navigation controller, use the Back button

        self.navigationItem.rightBarButtonItem = nil;

    }

If the view is the only view in its navigation controller, then I want the done button, otherwise, the user will use the navigation controller’s Back button to navigate away.