Disable rotation for View Controller in Navigation Controller

前端 未结 7 835
闹比i
闹比i 2020-12-19 12:57

First of all, this isn\'t a duplicate. I\'ve looked at all of the questions related to this on SO and none of them work for me. Hopefully it\'s just because I\'m new to iOS

7条回答
  •  囚心锁ツ
    2020-12-19 13:30

    In my case I have the view controller embedded in a navigation controller, and most of the solutions don't work because the viewcontroller depends on the navigation controller orientation. For this when you create the instance of the view controller you have to cast it to UINavigationController :

        let theViewController = UIViewController()
        if let navController = theViewController as? UINavigationController {
            navController.delegate = self
        }
    

    And then add this extension: extension PlaySplashViewController: UINavigationControllerDelegate {

    func navigationControllerSupportedInterfaceOrientations(_ navigationController: UINavigationController) -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait
    }
    

    }

    In this case this going to set portrait orientation for the navigation controller so the view controller going to use this orientation too.

提交回复
热议问题