1

I'm trying to create an app that only supports portrait orientation, I have tried setting at Target -> General -> Deployment Info -> Portrait

Then, at appDelegate.swift

override func shouldAutorotate() -> Bool {
        return false
    }

AT ViewController.swift

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return [UIInterfaceOrientationMask.Portrait ]
    }

But it doesn't work.. At ViewController.swift, it give me error:

Method does not override any method from its superclass

Joe
  • 8,868
  • 8
  • 37
  • 59
ramen87x
  • 160
  • 3
  • 18
  • Select a target and find Device Orientation under Deplayment info. – El Tomato Nov 26 '16 at 06:31
  • Have you unchecked all other orientations in Deployment Info except for Portrait? – fja Nov 26 '16 at 06:44
  • Yes I did, I even clear the landscape out at info.plist --> supported interface orientations – ramen87x Nov 26 '16 at 07:11
  • In Swift 3 this functions was replaced by an property. override var supportedInterfaceOrientations : UIInterfaceOrientationMask { return UIInterfaceOrientationMask.portrait } (note the var instead of func) – Thomas G. Nov 26 '16 at 08:20
  • @JohnFoong, checkout my post [here](http://stackoverflow.com/a/34489983/5576310) it will help you. – Rashwan L Nov 26 '16 at 08:22

2 Answers2

3

In Swift 3.1

If you want to lock orientation on all of the screen.

See also https://freakycoder.com/ios-notes-5-how-to-application-orientation-abba750bed6e .

Open AppDelegate.swift, and add this code.

// Lock the orientation to Portrait mode
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask(rawValue: UIInterfaceOrientationMask.portrait.rawValue)
}

or

// Lock the orientation to Landscape(Horizontal) mode
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask(rawValue: UIInterfaceOrientationMask.landscape.rawValue)
}
isseium
  • 625
  • 4
  • 11
1

Its a simple code just copy and paste the following code in the ViewController you want to fix the Orientation for.

override func shouldAutorotate() -> Bool {
    return false
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.Portrait
}

This works if and only if all the code is in the ViewController, no need to change anything or put anything in the AppDelegate. Cheers

Asfand Shabbir
  • 1,234
  • 13
  • 9