Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable and enable auto rotate on swift?

On general settings I allow portrait and landscapeleft , landscaperight modes. I want to turn off landscape modes. On viewController I write this code:

override func shouldAutorotate() -> Bool {

        return false

}

However, the auto rotation works ignoring this function. How do I disable and enable autorotation on swift? IOS programming

like image 452
Yestay Muratov Avatar asked May 07 '15 19:05

Yestay Muratov


People also ask

How do I stop my swift app from rotating?

Go it the main and select TARGETS then select Info tab (the plist) and open Supported inferface orientations (iPhone) the click on the ones that you do not need. Just leave Portrait(bottom home button) . That should make the UI stay one way.


3 Answers

I had the same problem, i have fixed that.

Follow-

info --> custom ios target properties --> Support Interface Orinetations. and delete ![Delete - Landscape (left home button), Landscape (right home button), Landscape (top home button)][1]

This will help you 😊

like image 104
Aman Jain Avatar answered Sep 19 '22 17:09

Aman Jain


It might be the right code, but not in the right View Controller. For example, if the View Controller is embedded in a UINavigationController, the navigation controller can still rotate, causing the View Controller to still rotate. It really depends on your specific situation.

like image 39
Josh Gafni Avatar answered Sep 19 '22 17:09

Josh Gafni


You can do it by creating a subclass of UINavigationController and inside that, override the should AutoRotate function,then

  • return false for the viewControllers in which you want to disable autorotation
  • return true for the viewControllers in which you want autorotation

    import UIKit    
    
    class CustomNavigationController: UINavigationController {
    
    override func shouldAutorotate() -> Bool {
        if !viewControllers.isEmpty {
            // Check if this ViewController is the one you want to disable roration on
            if topViewController!.isKindOfClass(ViewController) {               //ViewController is the name of the topmost viewcontroller
    
                // If true return false to disable it
                return false
            }
        }
        // Else normal rotation enabled
        return true
       }
    }
    

If you want to disable autorotation throughout the navigation controller, remove the if condition and return false always

like image 42
nikhil.g777 Avatar answered Sep 21 '22 17:09

nikhil.g777