简体   繁体   English

在一个视图中强制横向

[英]Force landscape orientation in one View

In my App all Views are forced to portrait orientation via info.plist.在我的应用程序中,所有视图都通过 info.plist 强制为纵向。

The exception should be "MyView" which should always be in landscape orientation.例外应该是“MyView”,它应该始终处于横向。

What I did after taking a deeper look in SO:在深入了解 SO 后我做了什么:

Added in AppDelegate.swift:在 AppDelegate.swift 中添加:

static var orientationLock = UIInterfaceOrientationMask.portrait 

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

MyView.swift 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 produces an error. UIDevice.current.setValue 产生错误。 Without that line I can change the orientation in MyView manually by holding the device in landscape/portrait but it should switch automatically when opening and closing the View.如果没有那条线,我可以通过将设备保持在横向/纵向状态来手动更改 MyView 中的方向,但它应该在打开和关闭视图时自动切换。

I solved this by orientating on bmjohns answer on a similar question:我通过针对类似问题的bmjohns 回答解决了这个问题:

  1. Add some code in AppDelegate (this part i had already)在 AppDelegate 中添加一些代码(这部分我已经有了)
  2. Make a struct and func which does the job (small adjustments for XCode 11 and SwiftUI were needed)制作一个 struct 和 func 来完成这项工作(需要对 XCode 11 和 SwiftUI 进行小幅调整)
  3. Execute the func onAppear of your View执行 func onAppear 你的视图

there is a way for Force landscape orientation有一种强制横向方向的方法

Put the code in viewDidLoad()将代码放入viewDidLoad()

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

and also,并且,

override var shouldAutorotate: Bool {
    return true
}

for SwiftUI用于 SwiftUI

We set the project orientation to only support Portrait mode.我们将项目方向设置为仅支持纵向模式。

Then in your AppDelegate add an instance variable for orientation and conform to the supportedInterfaceOrientationsFor delegate method.然后在您的 AppDelegate 中添加一个用于方向的实例变量并符合 supportedInterfaceOrientationsFor 委托方法。

static var orientationLock = UIInterfaceOrientationMask.portrait

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

Then when your about to present your landscape view perform the following actions:然后,当您要展示横向视图时,请执行以下操作:

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

And on dismissal,而在解雇时,

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

Hope this will help you ...:)希望能帮到你 ...:)

Since some info is missing on accepted answer i will put what i used in Swift 5由于接受的答案中缺少一些信息,我将把我在 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()
        })

Opening a view打开视图

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM