Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lock orientation of a viewController in swift

I'm a newbie in Swift and i have a problem locking the orientation to portrait in a viewController. Actually i have locked it using this code in my Custom Navigation Controller

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    if (self.visibleViewController is ViewController)
    {
        return UIInterfaceOrientationMask.Portrait
    }
    return .All
}

Everything works fine and the ViewController is locked to portrait.The problem is when return to this controller from another in landscape mode. if i return to ViewController (pressing back from the NextViewController) in landscape then the ViewController appeared in landscape. Any suggestion?

like image 530
aris ppspr Avatar asked May 15 '16 16:05

aris ppspr


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 lock orientation in Xcode?

Historically, if you wanted to restrict your iOS app to specific device orientations, you would check or uncheck the various “Device Orientation” options in your project settings. You can find these by selecting your Xcode Project > App Target > “General” tab. Xcode project "Device Orientation" options.

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.

How do you lock the orientation of a flutter?

Step 1: Open the screen in which you want to set the orientation. Step 2: Add the init() method and then call the SystemChrome. setPreferredOrientations method to set only landscape mode. Step 3: Add the dispose method and then call the SystemChrome.


1 Answers

In swift 3 this solution works

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if self.window?.rootViewController?.presentedViewController is LockedViewController {
        return UIInterfaceOrientationMask.portrait
    } else {
        return UIInterfaceOrientationMask.all
    }
}

Change LockedViewController to match the controller you would like locked.

like image 73
Ahmad Amin Avatar answered Sep 29 '22 09:09

Ahmad Amin