macCatalyst/ SwiftUI Touch Bar

北城余情 提交于 2020-07-18 06:46:25

问题


how could I add Touch Bar Support in Catalyst- Apps written in SwiftUI?

For Example, if I want to display a Button in a View:

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")
        }
    }
}

If I use it in a an macOS App it works but if I use it in Catalyst and add targetEnvironment it occurs the error:

'focusable(_:onFocusChange:)' is unavailable in iOS and 'touchBar(content:)' is unavailable in iOS

Thank you for help.


回答1:


Change

#if targetEnvironment(macCatalyst)

to

#if os(macOS)

macCatalyst must still be in the environment for iOS Mac Catalyst apps, so manually checking the OS should be reliable.



来源:https://stackoverflow.com/questions/59682434/maccatalyst-swiftui-touch-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!