ios - 如何在swiftui中硬定位锁定我的应用程序
问题描述
所以我正在制作一个完全与设备方向有关的应用程序 - 所以我不希望应用程序在我旋转它时以任何方式做出反应 - 即如果我在屏幕顶部放一个箭头,然后他们转动手机(或 iPad)向左,我希望箭头现在指向左侧 - 如果他们将手机倒置,我希望它指向下方。此外,如果他们将手机向左转 - 我希望几何阅读器仍然告诉我手机比它的宽度高得多。
我不知道该怎么做。我读过的所有内容都与 appdelegates 和视图控制器有关——而我只有一个带有视图的应用程序。
我尝试将其添加到 .plist 文件中:
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
我可以说它没有任何效果。
就我的应用而言,我怎样才能完全禁用任何旋转功能?
解决方案
试试这个:
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")
}
}
}
推荐阅读
- tensorflow - TensorBoard 未显示所有时期?
- c - LPC11C24 上的 WS2812 与 light_ws2812
- sql - 有没有其他方法可以加快 N 行的 INSERT INTO STATEMENTS 代码?
- python - 查找两个数组列表之间所有不同交集和差异的 Pythonic 方法
- android - 在 Dart 运行时和原生组件之间交换数据
- html - 在文本区域中按回车时反应论坛提交
- c# - 这是什么类型的关系以及如何在 .NET Core(EFCore、FluentAPI)中实现它?
- java - 安卓。如何知道允许的块大小(蓝牙)?
- tensorflow - 如何在不结束 12 小时限制的情况下在 Google Colab 中下载 7GB 的张量流数据集?
- python-3.x - ImportError:无法从“sklearn.externals”导入名称“joblib”