问题描述
我目前正在涉足 macOS 开发,并尝试将一些 AppKit 组件封装到 SwiftUI 中。我在 NavigationLink
中使用带有 List
的侧边栏导航。当使用 NSViewRepresentable
或 NSViewControllerRepresentable
作为目标时,makeNSView
/makeNSViewController
方法会被调用两次。我想这是一个错误,因为当我将 NavigationLink
直接包装在 NavigationView
中时它似乎工作正常?有没有其他人遇到过这个问题,可以帮我解决一个解决方法,直到它得到修复?
重现的最小实现:
import SwiftUI
struct ViewRepresentable: NSViewRepresentable {
let title: String
func updateNSView(_ nsView: NSView,context: Context) {
}
func makeNSView(context: Context) -> NSView {
print("\(title) make view called")
return NSView()
}
}
struct ContentView: View {
enum NavigationItem {
case sel1
case sel2
}
@State private var selection: NavigationItem? = .sel1
var body: some View {
NavigationView {
List(selection: $selection) {
NavigationLink(
destination: EmptyView().navigationTitle("Dashboard"),tag: NavigationItem.sel1,selection: $selection
) {
Label("Dashboard",systemImage: "globe")
}.tag(NavigationItem.sel1)
NavigationLink(
destination: ViewRepresentable(title: "TestView").navigationTitle("TestView"),tag: NavigationItem.sel2,systemImage: "globe")
}.tag(NavigationItem.sel2)
}
.padding(.top,16)
.navigationTitle("SomeApp")
.listStyle(SidebarListStyle())
}.frame(minWidth: 1000,idealWidth: 1200,maxWidth: .infinity,minHeight: 600,idealHeight: 800,maxHeight: .infinity,alignment: .center)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)