简体   繁体   中英

SwiftUI for MacOS (not Catalyst) - how to add buttons to navigation bar?

Goal: Add buttons to navigation bar on a real SwiftUI for MacOS (not Catalyst)

What I did: Tried navigationBarItem, but found it's not available for MacOS.

Does anyone have a solution? Browsed all possible MacOS SwiftUi projects and didn't find anything simulation. Should be trivial, as this is a very common use case?

在此处输入图片说明

import SwiftUI

struct SwiftUIView: View {
    var body: some View {


        NavigationView {

            Text("Hello, World!")

                .navigationBarItems()
        }





    }
}

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView()
    }
}

There is no such thing in macOS. You could use the NavigationLink. Check the following code:

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: Text("Detail").frame(maxWidth: .infinity, maxHeight: .infinity)) {
                Text("Hello, World!")
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
            }
        }
    }
}

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM