我在项目中遇到了类似的问题。它仅支持人像。ViewController的结构是,Navigation包含一个控制器(我称为A)和一个A控制器中的长Scrollview。我需要给B(风景右侧)展示A(人像)。
一开始,我尝试了以下方法,该方法似乎可行,但最终我发现了一个错误。
Swift 5和iOS12
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.landscapeRight
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .landscapeRight
}
然后,事情变得奇怪了。当控制器B向控制器A撤消时。控制器A中的ScrollView已滑到某个点。
因此,我使用了另一种方法,所以当时旋转屏幕viewWillAppear
。您可以在下面查看代码。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let appDel = UIApplication.shared.delegate as! AppDelegate
appDel.currentOrientation = .landscapeRight
UIDevice.current.setValue( UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
}
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
let appDel = UIApplication.shared.delegate as! AppDelegate
appDel.currentOrientation = .portrait
UIDevice.current.setValue( UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
super.dismiss(animated: true, completion: nil)
}
var currentOrientation : UIInterfaceOrientationMask = .portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return self.currentOrientation
}