【问题标题】:How to rotate screen with the `var shouldAutorotate` returns false如何使用 `var shouldAutorotate` 旋转屏幕返回 false
【发布时间】:2019-05-14 01:43:15
【问题描述】:

如何在不自动旋转的情况下设置指定的页面旋转?我发现只有当var shouldAutorotate返回yes时屏幕才能旋转,但我不想自动这样做,我想自己控制旋转的时刻

【问题讨论】:

  • 我设置了VC的静态变量canRotate,需要旋转的时候设置为true,旋转完成后设置为fals。 override var shouldAutorotate: Bool { return canRotate }
  • canRotate在收到UIDevice.orientationDidChangeNotification后设置为false,我发现在iphoneX,iOS 12.1的某个时候屏幕不能旋转orientationDidChangeNotification来了(虽然这是小概率事件)
  • 旋转屏幕代码:UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
  • 我在旋转代码之前解决了它:if !UIDevice.current.isGeneratingDeviceOrientationNotifications { UIDevice.current.beginGeneratingDeviceOrientationNotifications() }

标签: ios swift rotation screen shouldautorotate


【解决方案1】:

斯威夫特 4+:

在需要时使用以下代码旋转屏幕,无需任何自动旋转。

func rotateToLandscapeRight() {               
   UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
}

func rotateToLandscapeLeft() {
    UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
}

func rotateToPortratit() {                
    UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
}

【讨论】:

    猜你喜欢
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多