How to disable and enable automatic rotation at fast?

In the general settings, I enable portrait and landscape modes of landscape mode. I want to disable landscape modes. On viewController, I write this code:

override func shouldAutorotate() -> Bool { return false } 

However, automatic rotation works by ignoring this feature. How to disable and enable autorotation in swift? IOS programming

+6
source share
4 answers

This may be the correct code, but not in the correct View Controller. For example, if the view controller is built into the UINavigationController, the navigation controller can still rotate, causing the view controller to still rotate. It really depends on your specific situation.

+9
source

I had the same problem, I fixed it.

Follow -

info -> custom ios target properties -> Operation interface interface. and delete! [Delete - Landscape (left button of the house), Landscape (right button of the house), Landscape (top button of the house)] [1]

It will help you 😊

+3
source

You can do this by creating a subclass of UINavigationController and inside that, overriding the AutoRotate function, then

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

     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 in the entire navigation controller, remove the if condition and always return false

+3
source

Extending the answer of Josh Gaffney and user3655266, this concept also extends to view controllers.

If we have a UIViewController that is a child of another UIViewController in the view hierarchy, overriding the shouldAutorotate () child method to false can still rotate, since its parent controller can still return true . It is also important to know that although the child VC is displayed, the parent function shouldAutoRotate is still called. Therefore, control must 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 be done to allow only a specific orientation of the phone **

 override var supportedInterfaceOrientations: UIInterfaceOrientationMask { // This function is called on the parent 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 } 
0
source

All Articles