How to disable and enable auto rotate on swift?

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 😊


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.


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