Hi,
In Xcode 12.01
I am working on a View with an unknown number of DisclosureGroups in a List. I want to close any open DisclosureGroup when the user expands DisclosureGroups.
For example here in this test code I have 100 DisclosureGroups in List and the code will close any open.
import SwiftUI
struct TestView: View {
@State var selectedStrength = 0
@State var expaned = Array(repeating: false, count: 100)
var strengths = Array(repeating: "Test", count: 100)
var body: some View {
List {
ForEach(0 ..< strengths.count, id: \.self) { strIndex in
DisclosureGroup(isExpanded: $expaned[strIndex], content: {
Text("Group open \(strIndex)")
} , label: {
Text("Group \(strIndex)")
}).onChange(of: expaned[strIndex]) { _ in
if expaned[strIndex] {
for index in expaned.indices {
if index != strIndex {
expaned[index] = false
}
}
}
}
}
}
}
}
But how to handle this if the number number of DisclosureGroups not known?
Post
Replies
Boosts
Views
Activity
Hi,
in Xcode 12.0.1
I am using .contextMenu to show options to users. After moving from List to LazyVStack the preview of the the view changes to look strange.
The preview now is not full view but split-up. How can we control the view that .contextMenu show?
Also it seems .contextMenu deprecated in iOS 14+ - what is the alternative to .contextMenu?
I have this test code - try to show the context menu for the first row and see the preview spilt up with row and the strength Mild.
import SwiftUI
struct TestView: View {
@State var value: String = ""
var strengths = ["Mild", "Medium", "Mature"]
@State private var selectedStrength = 0
var body: some View {
VStack {
VStack {
Text("Header").font(.headline).padding(.top)
}
Divider()
VStack {
TestHeaderView().padding(.horizontal)
Divider()
ScrollView {
LazyVStack(alignment: .leading) {
ForEach(0 ..< strengths.count) {
TestRowView(event: self.strengths[$0]).contextMenu {
Text("Context")
}
}
}.padding(.horizontal)
}
}
}
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
TestView()
}
}
struct TestRowView : View {
let event: String
var gridItemLayout = [GridItem(.flexible(maximum: 20)), GridItem(.flexible()), GridItem(.flexible())]
var body: some View {
VStack {
LazyVGrid(columns: gridItemLayout, alignment: .leading) {
Image(systemName: "doc.plaintext").foregroundColor(.blue)
Text("Row")
Text(event)
}
Divider()
}
}
}
struct TestHeaderView : View {
var gridItemLayout = [GridItem(.flexible(maximum: 20)), GridItem(.flexible()), GridItem(.flexible())]
var body: some View {
LazyVGrid(columns: gridItemLayout, alignment: .leading) {
Text("").foregroundColor(.blue)
Text("Description1").foregroundColor(.blue)
Text("Description2").foregroundColor(.blue)
}
}
}