我的 macOS 应用程序有一个包含 3 列的 NavigationView,其中最左侧和最右侧的作为可以折叠的侧边栏。我设法通过放置一个带有 NSApp.keyWindow?.firstResponder?.tryToPerform(#selector(NSSplitViewController.toggleSidebar(_:)), with: nil)
的工具栏按钮使左侧边栏折叠起来。 ,但据我所知,它仅适用于拆分 View 的第一列。
如何将此行为复制到右侧边栏?
我的看法:
NavigationView {
switch loginInfo.loginStatus {
LoginView()
}
#if os(macOS)
if loginInfo.loginStatus != .notLoggedIn {
VStack {
CurrentlyTrackingView(loginInfo: loginInfo, user: user, presentsEditScreen: $showingEntryEditScreen).fixedSize()
TimeEntriesView()
}
VStack {
CurrentEntryView(loginInfo: loginInfo, user: user, entry: entrySidebar.entry, withName: entrySidebar.startName, bulkEdit: entrySidebar.bulkEdit, withStartDate: entrySidebar.startDate, withEndDate: entrySidebar.endDate)
Spacer()
}
}
#endif
}
.toolbar {
ToolbarItem(placement: .navigation) {
#if os(macOS)
Button(action: toggleLeftSidebar, label: {
Image(systemName: "sidebar.left")
})
#endif
}
}
func toggleLeftSidebar() {
#if os(macOS)
NSApp.keyWindow?.firstResponder?.tryToPerform(#selector(NSSplitViewController.toggleSidebar(_:)), with: nil)
#endif
}
最佳答案
如 Apple Forums 所示,尚无法使用 SwiftUI 实现
关于swift - 使用工具栏按钮折叠 SwiftUI 右侧边栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63024340/