Yestay Muratov
Yestay Muratov

Reputation: 1370

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

Upvotes: 13

Views: 18418

Answers (5)

Mr.SwiftOak
Mr.SwiftOak

Reputation: 1834

I am not sure if shouldAutorotate() function is still enabled in swift 5 for year 2021. However I recommend calling one of the following functions as part of standard procedure for managing rotation of ViewControllers. ("Handling View Rotations" part in apple developer webside For example preferredInterfaceOrientationForPresentation

Upvotes: 0

velociraptor11
velociraptor11

Reputation: 606

Extending Josh Gafni's answer and user3655266, the concept also extends to view controllers.

If we have a UIViewController that is a child of another UIViewController in the view hierarchy, overriding the child's shouldAutorotate() to false might still rotate since it parent's controller might still be returning true. It is also important to know that even though the child VC is being displayed, the parent's shouldAutoRotate function are still called. Hence the control should lie there.

Swift 5

class ParentViewController:UIViewController{ 
    override func shouldAutorotate() -> Bool {
        // Return an array of ViewControllers that are children of the parent
        let childViewControllersArray = self.children
        if childViewControllersArray.count > 0 {
            // Assume childVC is the ViewController you are interested in NOT allowing to rotate
            let childVC = childViewControllersArray.first
            if childVC is ChildViewController {
            return false
            }
        }
        return true 
    }
}

** The same can also be done to only allow certain phone orientation **

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        // This function is called on the parent's controller whenever the child of this parent is trying to rotate
        let childrenVCArray = self.children
        if childrenVCArray.count > 0 {
            // assuming the array of the first element is the current childVC
            let topMostVC = childrenVCArray[0]
            if topMostVC is ChildViewController {
                // Assuming only allowing landscape mode
                return .landscape
            }
        }
        // Return portrait otherwise
        return .portrait

    }

Upvotes: 0

nikhil.g777
nikhil.g777

Reputation: 904

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

Upvotes: 4

Aman Jain
Aman Jain

Reputation: 93

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 😊

Upvotes: 6

Josh Gafni
Josh Gafni

Reputation: 2881

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.

Upvotes: 15

Related Questions