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

Lor*_*bis 6 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("===")
     }
   }
Run Code Online (Sandbox Code Playgroud)

我希望我的问题很清楚。一如既往地非常感谢你。

添加了一些图像以显示部分标题具有关闭和打开按钮。

开关

小智 8

使用剖面的可折叠视图修改器。你的第一部分将变成:

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