How do I add a toolbar to a macOS app using SwiftUI?

前端 未结 4 1738
余生分开走
余生分开走 2021-02-05 15:32

I am trying to add a toolbar inside the title bar to a macOS app using SwiftUI, something similar to what is shown below.

I am unable to figure out a way to ac

4条回答
  •  不知归路
    2021-02-05 16:12

    As of macOS 11 you’ll likely want to use the new API as documented in WWDC Session 10104 as the new standard. Explicit code examples were provided in WWDC Session 10041 at the 12min mark.

    NSWindowToolbarStyle.unified
    

    or

    NSWindowToolbarStyle.unifiedCompact
    

    And in SwiftUI you can use the new .toolbar { } builder.

    struct ContentView: View {
    
    
      var body: some View {
            List {
                Text("Book List")
            }
            .toolbar {
                Button(action: recordProgress) {
                    Label("Record Progress", systemImage: "book.circle")
                }
            }
        }
    
        private func recordProgress() {}
    }
    

提交回复
热议问题