Mischa
Mischa

Reputation: 17269

Push and present view controllers with a single push animation

From my navigation controller's root view controller Root

How can I do both at the same time with a single push animation?

(💡 The idea behind this is that view controller A allows for editing some content. If no content has been created, it needs to show view controller B first which allows the user to enter a title and then create the content.)

What I've tried:

When I do the following in view controller A's viewDidLoad() method:

if content == nil {
    let createContentViewController = // instantiate new view controller instance
    present(createContentViewController, animated: false)
}

UIKit also omits the push animation when animated is set to false – so I get no animation at all. When animated is set to true, I get a double animation (first push, then modal). 🙄

Upvotes: 1

Views: 364

Answers (1)

matt
matt

Reputation: 535964

The reason you're having trouble doing what you describe is that you can't present View Controller B on View Controller A until View Controller A is part of the view controller hierarchy — and until you have pushed it, it isn't.

I would suggest, therefore, not using a presented view controller in this story at all. All you are really describing, it seems to me, is a View Controller A and its main view (View A) with a View B in front of it. That's something you can readily prepare between creating View Controller A and pushing it. View B can still contain a Dismiss button or similar to which you respond by sliding it off the screen, revealing View A.

If you really need a View Controller B for purposes of code organization, then have View Controller A be a custom parent view controller for View Controller B. A cool feature of that solution is that the whole thing can be configured in the storyboard using an embed segue. If we push View Controller A and you don't need View Controller B, you just hide View B before pushing.

Upvotes: 1

Related Questions