Search code examples
iosswiftorientationuibarbuttonitemuitoolbar

Is there a way to have a single view not rotate?


I am trying to create a view that doesn't rotate with device orientation. I want just this one view to not rotate because I have a UIToolbar that I don't want to rotate. Is this possible? If so, how do I implement it? I am using Swift. Also, is it possible to rotate the UIBarButtonItems with device orientation on the UIToolbar? If so, how do I implement that? Just to restate: I want the view and toolbar within it to not rotate with orientation; I want the buttons on the toolbar to rotate with orientation. Thanks.


Solution

  • I used the information from all of the answers here, as well as from this post overriding shouldAutorotate not working in Swift 3, to find the answer to my question. I created a MainNavigationController class within my MainViewController.swift file and then overrided shouldAutorotate like so:

    import UIKit
    
    class MainNavigationController: UINavigationController {
    
    override var shouldAutorotate: Bool {
       return false    }
    
    }
    
    class MainViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        }
    }
    

    The last step was to go into my Main.storyboard file, click on the navigation controller for the view, and change its class to MainNavigationController. Once I did that, it worked perfectly. Thanks to all the people who answered this question.