macos - SwiftUI 2 - 如何防止顶部部分在 macOS 和 iPadOS 中的列表中关闭

标签 macos swiftui ipados

在 SwiftUI 2 导航栏中,我有一个包含多个部分的列表。所有部分都有一个标题,但是我希望顶部部分永远不会被关闭并且元素永远不会被隐藏。这仅要求第一个标题,并且还希望显示指示符/折叠符号消失或隐藏。
这可以在 SwiftUI 2 中完成吗?
我想让它在 macOS 和 iPadOS 中工作。

@State var agendaViews: [String] = ["Agenda", "Client", "Next Client"]
@State var treatmentViews: [String] = ["Treatment", "Products", "Merchandising"]
@State var selectionAgenda: String?

var body: some View {
    NavigationView {
        VStack {
            List(selection: $selectionAgenda) {
                Section(header: ListHeader()) {    // <<<<<<< For this section no Close Icon and No way to close this section.
                    ForEach(agendaViews, id: \.self) { string in
                        NavigationLink(destination: DetailsView(test: string)) {
                            Text(string)
                        }
                    }
                }
                Section(header: ListHeader2(), footer: ListFooter2()) {
                    ForEach(treatmentViews, id: \.self) { string in
                        NavigationLink(destination: DetailsView2(test: string)) {
                            Text(string)
                        }
                    }
                }
            }.listStyle(SidebarListStyle())
    }
  }
}

struct ListHeader1: View {
  var body: some View {
      HStack {
        Image(systemName: "calendar")
        Text("Agenda")
      }
    }
  }
  struct ListHeader2: View {
  var body: some View {
     HStack {
        Image(systemName: "person.3")
        Text("Clienten")
     }
    }
   }

   struct ListFooter3: View {
     var body: some View {
       Text("===")
     }
   }
我希望我的问题很清楚。
一如既往地非常感谢你。
添加了一些图像以显示部分标题具有关闭和打开按钮。
Open & Close

最佳答案

对截面使用可折叠 View 修改器。您的第一部分将变为:

Section(header: ListHeader()) {
    ForEach(agendaViews, id: \.self) { string in
        NavigationLink(destination: DetailsView(test: string)) {
            Text(string)
        }
    }
}
.collapsible(false)

关于macos - SwiftUI 2 - 如何防止顶部部分在 macOS 和 iPadOS 中的列表中关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63045107/

相关文章:

swift - 删除 PencilKit 中的内容

macos - 苹果应用商店 : Is Sandboxing the App mandatory?

macos - 在 OS X 上运行 Delphi 10 Seattle 应用程序时出现 Dylib 版本错误

ios - SwiftUI:不要刷新自定义 View MacOS 应用程序

macos - SwiftUI:MacOS 上的 AppDelegate

ios - 如何通过在SwiftUI中触摸屏幕的任何位置来更改文本

ios - 如何替换当前的 CALayer

macos - 适用于 Mac 的轻量级本地主机文件服务器

ios - SwiftUI:添加核心数据项时更新选项卡栏角标(Badge)

ios - iOS/iPadOS 中的 SwiftUI + DocumentGroup : how to rename the currently open document