iosiphoneswiftscreen-orientation

Lock screen Orientation to portrait - ios swift


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


Solution

  • 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