18

I am creating an application with support only for portrait mode. I don't want Landscape or Portrait upside down. I tried some code. Which allows me to lock in portrait mode.

I am using navigationcontroller and presentviewcontroller. Now my problem is: 1. If I rotate my device upside down and open my application it opens in upside down mode which is wrong. 2. I click some button and enter to presentviewcontroller it returns to portrait mode.

I want all the navigationcontroller and presentviewcontroller in portrait mode

My codes:

I set device orientarion portrait in Target -> General -> Deployment Info -> Portrait

In my appdelagate.swift:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.Portrait
    }

In my First view controller. Which is basically child of Navigation Controller

override func shouldAutorotate() -> Bool {


            return false
        }

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

Edit 1:

Also I set StoryBoard -> ViewController -> Attribute Inspector -> Orientation -> Portrait

Edit 2:

My settings file enter image description here

Vvk
  • 4,031
  • 29
  • 51
Amsheer
  • 7,046
  • 8
  • 47
  • 81
  • check this one also its working for me [enter link description here](http://stackoverflow.com/questions/28938660/how-to-lock-orientation-of-one-view-controller-to-portrait-mode-only-in-swift) – Pradeep Kashyap Jan 17 '17 at 08:02

3 Answers3

40

Go To Target --> General and set Orientation Mode to Portrait.

enter image description here

Also check info.plist. Make sure Supported Interface Orientations contains only one value(Portrait). Sometime it removes from settings but not updated in plist file.

enter image description here

technerd
  • 14,144
  • 10
  • 61
  • 92
  • Please check Info.plist file as suggested in answer. This may be case with you. – technerd Feb 09 '16 at 06:47
  • Thanks Just I missed a single line and complete app went weird. I am new to ios thanks. – Amsheer Feb 09 '16 at 06:51
  • In my case there was an additional option for iPads that allowed all orientation types. So I left only the `Portrait (bottom home button)` and everything is working perfectly now. – Ivanka Todorova Apr 28 '18 at 09:25
3

If your app is not supporting split screen view than you should check the option to Require full screen. This fixed my issue with portrait mode. enter image description here

If this does not resolve issue add Supported interface orientations Key of Array type in your info.plist. And String item to that array with value Portrait (bottom home button)

enter image description here

Saqib Omer
  • 5,387
  • 7
  • 50
  • 71
  • Thanks Just I missed a single line and complete app went weird. I am new to ios thanks. Technerd add solutios 1 minute before you So I accept his answer. I upvoted You. – Amsheer Feb 09 '16 at 06:52
0

For me the only working solution was to add:

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    .portrait
}

in the app's delegate class, as indicated in the question itself.

I'm using SwiftUI without any storyboard file, even for the splashscreen.

Domenico
  • 1,331
  • 18
  • 22