Skip to content

Instantly share code, notes, and snippets.

@iAmrSalman
Created August 26, 2017 20:54
Show Gist options
  • Select an option

  • Save iAmrSalman/cc2412d07c5ce1e1e7cffc87f2b0c31d to your computer and use it in GitHub Desktop.

Select an option

Save iAmrSalman/cc2412d07c5ce1e1e7cffc87f2b0c31d to your computer and use it in GitHub Desktop.
[Detect orientation change] #swift3

Here's how I got it working:

In AppDelegate.swift inside the didFinishLaunchingWithOptions function I put:

NotificationCenter.default.addObserver(self, selector: #selector(AppDelegate.rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)

and then inside the AppDelegate class I put the following function:

func rotated() {
    if UIDeviceOrientationIsLandscape(UIDevice.current.orientation) {
        print("Landscape")
    }

    if UIDeviceOrientationIsPortrait(UIDevice.current.orientation) {
        print("Portrait")
    }

}

Hope this helps anyone else!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment