Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 347
Amsheer Avatar asked Feb 09 '16 06:02

Amsheer


People also ask

How do I lock device orientation in Swift?

Set the supportedInterfaceOrientations property of specific UIViewControllers like this: class MyViewController: UIViewController { var orientations = UIInterfaceOrientationMask. portrait //or what orientation you want override var supportedInterfaceOrientations : UIInterfaceOrientationMask { get { return self.

How do I change the orientation of my iPhone lock screen?

Swipe down from the top-right corner of your screen to open Control Center. Tap the Portrait Orientation Lock button to make sure that it's off. Turn your iPhone sideways.

How do I turn off landscape mode in Swift?

From the main screen, slide the top of the screen down. Then, slide the top of the screen down again. Tap the "Portrait" icon to enable landscape mode, or tap the "Auto rotate" icon to disable it. Note: When "Auto rotate" is enabled, the interface will rotate when you hold the phone horizontally.


1 Answers

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

like image 64
technerd Avatar answered Oct 04 '22 07:10

technerd