Попробуйте это
Первый ViewController
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
if UIDevice.current.orientation.isLandscape {
print("Landscape")
let vc2 = self.storyboard?.instantiateViewController(withIdentifier: "ViewController2")
self.navigationController?.pushViewController(vc2!, animated: false)
} else {
print("Portrait")
}
}
override func viewDidAppear(_ animated: Bool) {
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
Второй ViewController
class ViewController2: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
if UIDevice.current.orientation.isLandscape {
print("Landscape")
} else {
print("Portrait")
self.navigationController?.popViewController(animated: false)
}
}
}
Вы обнаружите изменение ориентации в методе viewWillTransition, поэтому вам нужно переопределить его