modal view controller animates on state restoration
Asked Answered
C

1

7

I have a strange problem with state restoration for a Universal app with Split View Controller.

The strange thing that I am doing things in a very standard way using a Storyboard and segues and with a restoration identifier for alle relevant view controllers. There is not really any code, as the logic is in the Storyboard and a minimal XCode project shows this.

The problem is with a settings screen that is shown modally as a form sheet presented from the split view controller. My view controller hierarchy ends up correct, but the transition doesn't really make sense. For some reason state restoration animates the modal controller into place.

Since the screen starts out with a screenshot from the last time the app was running, with the settings controller already present, the animation is just visual noise.

I have tried to disable animation on the segue which is respected when entering the settings interactively, but when state restoration does the same thing, the animation is there.

What is the standard way to avoid this?

Cumbrance answered 22/8, 2016 at 11:51 Comment(0)
D
12

Calling self.window?.makeKeyAndVisible() in application(_:willFinishLaunchingWithOptions:) solved the issue for me.

More info in the docs:

Important

If your app relies on the state restoration machinery to restore its view controllers, always show your app’s window from this method. Do not show the window in your app’s application:didFinishLaunchingWithOptions: method. Calling the window’s makeKeyAndVisible method does not make the window visible right away anyway. UIKit waits until your app’s application:didFinishLaunchingWithOptions: method finishes before making the window visible on the screen.

Discrown answered 17/4, 2017 at 9:23 Comment(3)
I was really skeptic about this, but I'm impressed on how well it worked. Thank you very much! Before that we were checking if the previous viewcontrollers were created by restore and disabling their present animationCivet
In my case I wasn't calling self.window.makeKeyAndVisible() anywhere but I was setting window.rootController in application:didFinishLaunchingWithOptions: method. Moving this to application:willFinishLaunchingWithOptions: did the trick. Thanks!Scylla
This solution is great for iOS 12 but is not possible for iOS 13 since window is not set in the application delegate. Any thoughts on how this solution can be adapted for iOS 13 (using scenes)?Handfast

© 2022 - 2024 — McMap. All rights reserved.