I managed to get navigationControllerSupportedInterfaceOrientations called during the start up, but then, when I push a new controller, it´s not called (it seems that if you declare supportedInterfaceOrientations in the navigation controller, the delegate method is not called).
//Initial controller. PortaitViewController is the top view controller in CustomNavigationController. When I push LandscapeViewController, I need the device to rotates automatically to landscape.
class PortaitViewController: UIViewController {
@IBAction func landscapeTUI(_ sender: Any) {
let vc = LandscapeViewController(nibName: "LandscapeViewController", bundle: nil)
self.navigationController?.pushViewController(vc, animated: true)
}
override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
return .portrait
}
override var shouldAutorotate : Bool {
return true
}
}
class LandscapeViewController: UIViewController {
override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
return .landscapeLeft
}
override var shouldAutorotate : Bool {
return true
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .landscapeLeft
}
}
class CustomNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
}
extension CustomNavigationController: UINavigationControllerDelegate {
func navigationControllerSupportedInterfaceOrientations(_ navigationController: UINavigationController) -> UIInterfaceOrientationMask {
return self.topViewController?.supportedInterfaceOrientations ?? .portrait
}
}