Displaying a view in two ways (push and modally)

I'm using a storyboard. I have aUINavigationController, with aUITableViewControlleras itsrootViewController. From this table view, the user can access theDetailViewController(a customUIViewControllersubclass) in two ways:

  1. tap a table cell to view its information
  2. tap the plus button to create a new item

For way 1, I want my segue to be pushed. For way 2, I want the details to be displayed modally.

At first I thought this shouldn't be too difficult, but I was opposed with some trouble. What caused this trouble: the navigation bar. When the next view is pushed, the navigation bar is there. When the next view is displayed modally, however, the navigation bar isn't there.

I know that Apple has thought about this and this is the most logical approach, but I need the navigation bar in my modal view to dismiss the modal view again (using a cancel and a done button). Adding this navigation bar through interface builder brings no luck: for the modally displayed view it works fine, but in my pushed view, I suddenly have two navigation bars.

The logical solution to this would be to add the navigation bar programmatically,onlyif the view is displayed modally. I did this, and the navigation bar was displayed, but it overlapped the view. I tried resizing my view so it has the same size as the view that's being pushed, but I did not manage to solve the problem: the navigation bar was still overlapping the view.

There are two reasons I stopped trying at this point: I didn't know what to try anymore, and I felt like there should be a much easier way, as I'm probably not the only one trying to achieve this.

Can anyone help me out?

You can create another navigation controller with your detail view controller as its root view controller. Then present that new navigation controller modally using your main view controller as the presenter.

What Others Are Reading