首页 > 解决方案 > 如何在swiftui中硬定位锁定我的应用程序

问题描述

所以我正在制作一个完全与设备方向有关的应用程序 - 所以我希望应用程序在我旋转它时以任何方式做出反应 - 即如果我在屏幕顶部放一个箭头,然后他们转动手机(或 iPad)向左,我希望箭头现在指向左侧 - 如果他们将手机倒置,我希望它指向下方。此外,如果他们将手机向左转 - 我希望几何阅读器仍然告诉我手机比它的宽度高得多。

我不知道该怎么做。我读过的所有内容都与 appdelegates 和视图控制器有关——而我只有一个带有视图的应用程序。

我尝试将其添加到 .plist 文件中:


    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
    </array>

我可以说它没有任何效果。

就我的应用而言,我怎样才能完全禁用任何旋转功能?

标签: iosswiftui

解决方案


试试这个:

import SwiftUI

@main
struct TestApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait
    }
}
struct ContentView: View {
    var body: some View {
        VStack (spacing: 50) {
            Text("testing orientation portrait only")
            Text("testing orientation portrait only")
        }
    }
}

推荐阅读