I am trying to create a sidebar that contains multiple DisclosureGroup
s where some of them contain a nested list. The whole sidebar when fully expanded will be longer than the screen, so it needs to scroll.
My test code for macOS looks like this:
import SwiftUI
class MenuItem: Identifiable {
var id = UUID()
var text : String
var children : [MenuItem]? = [MenuItem]()
init(text: String) {
self.text = text
}
}
struct ContentView: View {
var menuItems = [MenuItem]()
init() {
for itemText in ["One", "Two", "Three", "Four", "Five"] {
menuItems.append(MenuItem(text: itemText))
}
for item in menuItems {
for childText in ["A", "B", "C", "D", "E"] {
item.children?.append(MenuItem(text: childText))
}
}
}
var body: some View {
ScrollView {
DisclosureGroup("Section 1") {
List(menuItems, children: \.children) { item in
Text(item.text)
} }
DisclosureGroup("Section 2") {
Text("Some other stuff")
}
DisclosureGroup("Section 3") {
List(menuItems, children: \.children) { item in
Text(item.text)
}.frame(minHeight: 300)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
The problem with this code is that Section 2 expands as expected, while section 1 does not expand at all. I can make a nested list show up by giving it a fixed frame (like in section 3), but then I get a scrolling list within a scrolling sidebar and the frame does not adjust to the size of the expanded nested list.
My aim is to have one big scrolling sidebar with three DisclosureGroup
s that collapses, expands and scrolls as one unit.