在一个视图中强制横向

Force landscape orientation in one View

在我的应用程序中,所有视图都通过 info.plist 强制为纵向。

例外情况应该是 "MyView",它应该始终处于横向。

深入了解 SO 后我做了什么:

在AppDelegate.swift中添加:

static var orientationLock = UIInterfaceOrientationMask.portrait 

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return AppDelegate.orientationLock
}

MyView.swift

struct MyView: View {
var body: some View {
    ZStack {
        // ...Some Images, etc.
    }
    .onAppear(perform: orientationLandscape)
    .onDisappear(perform: orientationPortrait)
}

func orientationLandscape() {
    AppDelegate.orientationLock = UIInterfaceOrientationMask.landscapeRight
    UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight, forKey: "orientation")
    UINavigationController.attemptRotationToDeviceOrientation()
}

func orientationPortrait() {
    AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
    UIDevice.current.setValue(UIInterfaceOrientation.portrait, forKey: "orientation")
    UINavigationController.attemptRotationToDeviceOrientation()
}

}

UIDevice.current.setValue 产生错误。 如果没有那条线,我可以通过将设备保持在 landscape/portrait 中手动更改 MyView 中的方向,但它应该在打开和关闭视图时自动切换。

有一种强制横屏的方法

把代码放在viewDidLoad()

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

还有,

override var shouldAutorotate: Bool {
    return true
}

对于 SwiftUI

我们将项目方向设置为仅支持纵向模式。

然后在您的 AppDelegate 中添加一个方向实例变量并符合 supportedInterfaceOrientationsFor 委托方法。

static var orientationLock = UIInterfaceOrientationMask.portrait

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return AppDelegate.orientationLock
}

然后当您要呈现横向视图时,请执行以下操作:

AppDelegate.orientationLock = UIInterfaceOrientationMask.landscapeLeft
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()

解雇时,

AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
UIDevice.current.setValue(UIInterfaceOrientation.portrait, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()

希望这对您有所帮助...:)

我通过针对 解决了类似问题:

  1. 在 AppDelegate 中添加一些代码(这部分我已经有了)
  2. 制作一个完成工作的结构和函数(需要对 XCode 11 和 SwiftUI 进行小的调整)
  3. 执行视图的 onAppear 函数

由于接受的答案缺少一些信息,我将把我使用的内容放在 Swift 5

class AppDelegate: UIResponder, UIApplicationDelegate {


    var orientationLock = UIInterfaceOrientationMask.portrait

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return orientationLock
    }
}

struct AppUtility {

    static func orientationLandscape() {
        if let delegate = UIApplication.shared.delegate as? AppDelegate {
                delegate.orientationLock = UIInterfaceOrientationMask.landscapeRight
        }
        UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
        UINavigationController.attemptRotationToDeviceOrientation()
    }
    
    static func orientationPortrait() {
        if let delegate = UIApplication.shared.delegate as? AppDelegate {
                delegate.orientationLock = UIInterfaceOrientationMask.portrait
        }
        UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
        UINavigationController.attemptRotationToDeviceOrientation()
    }

}

.onAppear(perform: {
            AppUtility.orientationLandscape()
        }).onDisappear(perform: {
            AppUtility.orientationPortrait()
        })

打开视图

.fullScreenCover(isPresented: $isPresented) {
                                NavigationLazyView(aView())
                            }