macCatalyst/ SwiftUI 触控栏

TS2*_*251 5 macbookpro-touch-bar swiftui mac-catalyst

如何在用 SwiftUI 编写的 Catalyst 应用程序中添加触控栏支持?

例如,如果我想在视图中显示一个按钮:

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack{
        #if targetEnvironment(macCatalyst)
            Text("macOS")
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .focusable()
            .touchBar {
                Button(action: {
                    print("tapped")
                }) {
                    Text("TestButton")
                }
            }
        #endif
        Text("iOS")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我在 macOS 应用程序中使用它,它可以工作,但如果我在 Catalyst 中使用它并添加 targetEnvironment,则会出现错误:

“focusable(_:onFocusChange:)”在 iOS 中不可用,“touchBar(content:)”在 iOS 中不可用

谢谢你的帮助。

Ben*_*enj 0

改变

#if targetEnvironment(macCatalyst)
Run Code Online (Sandbox Code Playgroud)

#if os(macOS)
Run Code Online (Sandbox Code Playgroud)

macCatalyst必须仍处于 iOS Mac Catalyst 应用程序的环境中,因此手动检查操作系统应该是可靠的。