Search code examples
iosiphoneswiftipad

Weird bug when presenting a view controller


I have a basic app with a UITabBarController as the root view controller. When a user of the app is not signed in I'm showing a modal controller via window!.rootViewController!.present(viewController, animated: true) in my AppDelegate. This works fine on all iPhone models, however the following happens on any iPad:

broken

The background color of the SignInController is visible during the transition. Now comes the weird thing: When I change the view in Interface Builder to an iPad the bug is gone like so:

working

Changing the background color back to the transparent default removes at least the white background, however the view is still animating from the left bottom which is something I don't want. And by the way, changing the view in Interface Builder breaks the animation on all iPhones. Changing it back fixes it but breaks again all iPads.

This is the code (using ReSwift for state management):

func newState(state: State) {
  switch (previousState.session, state.session) {
  case (.loading, .notSignedIn), (.signedIn, .loading):
      (window!.rootViewController! as! UITabBarController).selectedIndex = 0

      let viewController = storyboard.instantiateViewController(withIdentifier: "SignInViewController")
      window!.rootViewController!.present(viewController, animated: true, completion: nil)

  default:
    // more stuff
    break
  }
}

EDIT: Added the actual code.


Solution

  • I fixed it! 😊

    The problem was a combination of having an observer on keyboardWillShowNotification and a becomeFirstResponder in the viewWillAppear method of the presented controller.

    Moving the becomeFirstResponder into viewDidAppear fixed all the problems!