当我更改根视图控制器时,屏幕方向出现问题。我在之前的root上呈现VC并且在旋转后更改rootVC并解除呈现VC。我存储了它们,在屏幕旋转到纵向模式后,我将它们恢复到屏幕上。我的流程是下一个:RootViewController - > presentViewController - > rotateToLandscape - > dissmissPresentedViewController - > createFullScreenViewController - > changeRootToFullScreenViewController - > rotateToPortrait - > changeRootToStored - > presentStoredVC。
但是当我恢复rootViewController时,它具有横向方向,而设备处于纵向模式。
以下代码:
PresentedViewControllerHelper:
class PresentedControllerHelper: NSObject {
public static let sharedInstance = PresentedControllerHelper()
public var rootViewController: UIViewController!
public var presentedViewController: UIViewController!
private override init() { }
func openFullScreen() {
self.presentedViewController.dismiss(animated: false, completion: {
self.rootViewController = UIApplication.shared.keyWindow?.rootViewController
UIApplication.shared.keyWindow?.rootViewController = NewRootController()
})
}
func openInline() {
UIApplication.shared.keyWindow?.rootViewController = rootViewController
self.rootViewController.present(presentedViewController, animated: false, completion: nil)
}
}
NewRootViewController
class NewRootController: UIViewController {
override func viewDidLoad() {
self.view.backgroundColor = .red
}
}
PresentedViewController
class PresentedViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
PresentedControllerHelper.sharedInstance.presentedViewController = self
}
deinit {
print("sdsfglkdnfgklnsdflkng")
}
@objc func deviceOrientationChanged() {
let orientation = UIDevice.current.orientation
if (orientation == .landscapeLeft || orientation == .landscapeRight) {
PresentedControllerHelper.sharedInstance.openFullScreen()
} else if (orientation == .portrait || orientation == .unknown) {
PresentedControllerHelper.sharedInstance.openInline()
}
}
怎么能修好?有什么想法吗?