Skip to content

Instantly share code, notes, and snippets.

@chenr2
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chenr2/bfcf66cbaad79343d270 to your computer and use it in GitHub Desktop.
Save chenr2/bfcf66cbaad79343d270 to your computer and use it in GitHub Desktop.

Disallow rotation for onboarding

Assumptions:

  • You're using UIPageViewController for onboarding
  • You have an UIViewController with a container that embeds the UIPageViewController
  • You present this UIViewController modally

The UIViewController container lets you do two things:

  • Add a close button that's always visible, and doesn't scroll with the UIPageViewController
  • Prevent rotation to landscape
  • Rotate to portrait if the user was already in landscape to begin with
// UIViewController

override func viewWillAppear(animated: Bool) {
  if UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation) {
    let value = UIInterfaceOrientation.Portrait.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")
  }
}

override func shouldAutorotate() -> Bool {
    return !UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment