当子类化UINavigationController
时,如何在已经被推入堆栈的视图控制器上强制设备旋转?我试图解决的问题是,如果在纵向模式下将视图控制器推入堆栈,例如,即使设备已旋转到横向,该视图控制器也只会以纵向模式弹出。因此,如何强制目标视图控制器(在弹出过渡期间)旋转其方向?
以下代码来自自定义弹出过渡动画,该动画将发生强制方向更改。
...
guard let fromViewController = topViewController,
let toViewController = previousViewController else {
return
}
...
// force rotation before popping
if UIDevice.current.orientation == .portrait {
// this does not work
let value = UIInterfaceOrientationMask.portrait
toViewController.setValue(value, forKey: "supportedInterfaceOrientations")
} else if UIDevice.current.orientation == .landscapeLeft {
// force toViewController into landscape left
} else if UIDevice.current.orientation == .landscapeRight {
// force toViewController into landscape right
}
// begin pop animation
...