如何在 Swift 中打开 on/off 纵向方向锁定 iPhone

How to turn on/off Portrait Orientation Lock iPhone in Swift

我已经使用我的代码设置了屏幕的亮度。

UIScreen.main.brightness = CGFloat(0.5)

我可以使用代码启用和禁用 iPhone 的屏幕方向锁定吗?

感谢大家的帮助!

第 3 方应用程序无法操纵所有系统设置或其他应用程序设置,因为这可能会导致安全漏洞。这叫做沙盒。每个应用程序都有它自己的“沙盒”,或者简单地说,一个监狱,在那里它无法 access/manipulate 系统设置或其他应用程序设置,这使 Apple 设备真正安全。

尽管您可以使用

操纵应用程序的方向
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}

• 将 shouldAutorotate 设置为 False

 override open var shouldAutorotate: Bool {
    return false
 }

• 指定方向。

override open var supportedInterfaceOrientations:UIInterfaceOrientationMask {
    return .portrait
}

P.S :您可以在 您的 应用程序的屏幕上执行此操作,而不是在系统上执行此操作 (iPhone)。

您可以使用它来更新设备方向:

UIDevice.current.setValue(UIDeviceOrientation.portrait.rawValue, forKey: "orientation")

或者

UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")