如何在MacOS多列导航视图中显示/隐藏列?
我想在 macOS 上显示/隐藏 3 列 NavigationView 布局中的第二列。切换按钮确实隐藏了第二列的内容 - 但我希望它完全消失,就像用户将其拖走一样。
有什么想法吗?
struct ContentView: View {
@State private var showSecondColumn = true
var body: some View {
NavigationView {
// 1. Column / sidebar
List {
ForEach(0..<10) { i in
Text("Sidebar \(i)")
}
}
.listStyle(.sidebar)
// 2. Column - conditional
if showSecondColumn {
List {
ForEach(0..<10) { i in
Text("Second \(i)")
}
}
// .frame(width: showSecondColumn ? 150 : 0) // does not work either
}
// 3. Column / Content
Button(showSecondColumn ? "Hide second" : "Show second") {
withAnimation {
showSecondColumn.toggle()
}
}
}
}
}
I would like to show/hide the 2nd column in a 3 column NavigationView layout on macOS. The toggle button does hide the content of the 2nd column – but I would like it to vanish completely, as if the user drags it away.
Any ideas?
struct ContentView: View {
@State private var showSecondColumn = true
var body: some View {
NavigationView {
// 1. Column / sidebar
List {
ForEach(0..<10) { i in
Text("Sidebar \(i)")
}
}
.listStyle(.sidebar)
// 2. Column - conditional
if showSecondColumn {
List {
ForEach(0..<10) { i in
Text("Second \(i)")
}
}
// .frame(width: showSecondColumn ? 150 : 0) // does not work either
}
// 3. Column / Content
Button(showSecondColumn ? "Hide second" : "Show second") {
withAnimation {
showSecondColumn.toggle()
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于遇到此问题的其他人,我也遇到了同样的问题。我发现这篇文章基本上说列的数量在编译时是固定的,不能改变动态变化。
有点令人失望,但就其价值而言,苹果自己的笔记应用程序在未选择任何内容时只有一个空白的第三列,所以我想这是设计的一部分。
I ran into this same issue for anyone else that comes across this. I found this article basically saying the amount of columns is fixed at compile time and cannot be changed dynamically.
A bit disappointing, but for what it's worth Apple's own notes app just has a blank third column when nothing is selected so I guess it's part of the design.