如何在方向被另一个(iOS)强制横向化后恢复UIView控制器的右侧方向?



我遇到的问题有点奇怪,我似乎找不到任何好的解决方案。所以我有两个UIView控制器,一个允许采取所有方向,另一个只能横向查看

internal override func shouldAutorotate() -> Bool {
return true
}
internal override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Lanscape
}

如果我在 ViewController1 上处于纵向模式,并且我按下 VC2,它会很好地旋转,但是当我离开 VC2 时,即使设备本身处于纵向模式,VC1 也会卡在横向状态。我该如何解决它?我尝试在VC1的ViewDidAppear方法中调用TryToRotateToDeviceOrientation方法,但它没有做任何事情

您只需要在视图控制器中创建应用程序委托实例,如下所示:

let appDel = UIApplication.shared.delegate as! AppDelegate

在视图控制器的视图上将出现((只需添加此代码即可横向

appDel.myOrientation = .landscape
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")

对于肖像,请添加此代码

appDel.myOrientation = .portrait
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")

最新更新