SwiftuUI NavigationLink inside touchBar

情到浓时终转凉″ 提交于 2021-02-11 08:55:14

问题


I'm trying to create NavigationLink in MacBook touchBar with help of SwiftUI. Actually with my piece of code, the button is shown in touchbar, but unfortunately the link doesn't work.

NavigationView {
  .touchBar {
    NavigationLink(destination: BookView()) {
      Text("GoToBook")
    }
  }
}

struct BookView: View {
  var body: some View {
    Text("Hello")
  }
}

回答1:


Try instead with Button in touchBar activating NavigationLink programmatically, like below

@State private var isActive = false

...

// below in body
NavigationView {
  SomeView()      // << your view here 
  .background(NavigationLink(destination: BookView(), isActive: $isActive) { 
                EmptyView() 
              }  // hidden link
  )
  .touchBar {
     Button("GoToBook") { self.isActive.toggle() } // activate link
  }
}


来源:https://stackoverflow.com/questions/61178376/swiftuui-navigationlink-inside-touchbar

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