【问题标题】:Is there a targetEnvironment for macCatalyst in SwiftUI?SwiftUI 中有 macCatalyst 的 targetEnvironment 吗?
【发布时间】:2023-04-08 13:19:02
【问题描述】:

如何在 SwiftUI 中使用用于检查 macCatalyst 的 targetEnvironment 创建条件视图?

类似:

#if targetEnvironment(macCatalyst)
    print("macOS")
#else
    print("Your regular code")
#endif

但直接在 SwiftUI 中

【问题讨论】:

  • 这可能取决于上下文,所以你会显示你的代码吗?你想有条件地在哪里显示什么?
  • 我只想在 Mac 应用而不是 iPhone/iPad 应用中显示按钮

标签: swiftui target catalyst


【解决方案1】:

struct ContentView: View {
    var body: some View {
        VStack {
#if targetEnvironment(macCatalyst)
            Button("Catalyst Demo") { }
#endif
            Text("Hello, World!")
        }.frame(width: 300, height: 200)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-23
    • 2019-12-30
    • 2021-03-22
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多