【问题标题】:How to restore right orientation of the UIViewController after orientation was forced to landscape by another (iOS)?方向被另一个(iOS)强制横向后如何恢复 UIViewController 的正确方向?
【发布时间】:2020-02-11 09:03:58
【问题描述】:

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

internal override func shouldAutorotate() -> Bool {
    return true
}

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

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

【问题讨论】:

  • @mamun999 你可能误解了我的意思。我不是在问如何强制定向,我的解决方案已经在工作。我在问如何解决它的副作用

标签: ios swift uiinterfaceorientation interface-orientation


【解决方案1】:

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

let appDel = UIApplication.shared.delegate as! AppDelegate

在视图控制器的 Viewwillappear() 上,只需为横向添加此代码

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

对于肖像添加此代码

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

【讨论】:

  • 这不是我所需要的。我需要每个控制器都能自动旋转到所有方向,但其中一个只能是横向的。我通过我的解决方案得到了这一点,但以前的控制器由于某种原因卡在了风景中
  • 好吧,只要在需要的地方做点横向添加此代码appDel.myOrientation = .landscape UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation") 并在离开该屏幕的同时将旋转设置为各个方面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-21
  • 2016-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-30
相关资源
最近更新 更多