Disable Rotation of UIViewController

In this short Swift code example, you will learn how to disable rotation of the UIViewController if device is rotated right or left. Note, that if your view is embedded into UINavigationController a different approach is needed. Check Swift Code Examples page to learn how to disable rotation of the view if it is embedded into UINavigationController.

  • We will need to override shouldAutorotate function and return a value of false

Disable Rotation of UIViewController Code Example in Swift

import UIKit
class ViewController: UIViewController  {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    override open var shouldAutorotate: Bool {
        return false
    }
 
}

For more Swift code examples and tutorials, please check the Swift Code Examples page on this website.


Leave a Reply

Your email address will not be published. Required fields are marked *