如何在MacOS多列导航视图中显示/隐藏列?

发布于 2025-01-17 05:35:24 字数 1342 浏览 2 评论 0原文

我想在 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?

enter image description here

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆躺在深渊里 2025-01-24 05:35:24

对于遇到此问题的其他人,我也遇到了同样的问题。我发现这篇文章基本上说列的数量在编译时是固定的,不能改变动态变化。

有点令人失望,但就其价值而言,苹果自己的笔记应用程序在未选择任何内容时只有一个空白的第三列,所以我想这是设计的一部分。

输入图片此处描述

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.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文