SwiftUI 在所有视图上强制人像,除了一个视图

ber*_*glu 13 landscape portrait orientation ios swiftui

我有一个 SwiftUI 项目。对于除一种视图之外的所有视图,我想允许纵向和仅纵向模式。对于只有一种视图,我想同时允许纵向和横向。Swift 上有一些资源,但我在 SwiftUI 上找不到任何资源。

有没有人找到一种方法来实现这一点?

小智 22

我不得不做类似的事情。这是我们的方法。

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

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

static var orientationLock = UIInterfaceOrientationMask.portrait

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

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

AppDelegate.orientationLock = UIInterfaceOrientationMask.landscapeLeft
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
Run Code Online (Sandbox Code Playgroud)

而在解雇时,

AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

  • 这仅将设备锁定为横向模式,但 OP 要求允许纵向和横向视图。是否有解决方案可以在此视图中同时允许纵向和横向? (5认同)

Zen*_*n C 16

对上述答案的一些调整:

在 AppDelegate 中作为乔纳森上面的回答:

static var orientationLock = 
UIInterfaceOrientationMask.portrait

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

然后在“destinationView”中 - 风景:

import SwiftUI

struct DestinationView: View {

var body: some View {
    Group {

        Text("Hello")

    }.onAppear {
        AppDelegate.orientationLock = UIInterfaceOrientationMask.landscapeLeft
        UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
        UINavigationController.attemptRotationToDeviceOrientation()
    }
    .onDisappear {
        DispatchQueue.main.async {
            AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
            UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
            UINavigationController.attemptRotationToDeviceOrientation()
        }
    }
}
}
Run Code Online (Sandbox Code Playgroud)

请注意 UIDevice 行中的 .rawValue 消除了“LongValue”错误。此外,在 .onDisappear 中,我必须使用 DispatchQueue.main.async 以避免在返回到纵向视图时出现错误。


vib*_*oto 9

如果您的应用程序使用 SwiftUI lifeCycle,则应用程序将使用符合应用程序协议的自定义结构启动。您的应用程序没有实施上述解决方案所需的 AppDelegate。

要制作您自己的 AppDelegate,请在您的 appnameApp.swift 中尝试此代码

@main
struct appnameApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

class AppDelegate: NSObject, UIApplicationDelegate {
    
    static var orientationLock = UIInterfaceOrientationMask.portrait
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        // something to do
        return true
    }
    
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window:UIWindow?) -> UIInterfaceOrientationMask {
        return AppDelegate.orientationLock
    }
}
Run Code Online (Sandbox Code Playgroud)

来源:https ://www.hackingwithswift.com/quick-start/swiftui/how-to-add-an-appdelegate-to-a-swiftui-app