ios SwiftUI 2.0 List with children -如何使显示按钮的可点击区域覆盖整个列表项

aoyhnmkz  于 2022-12-15  发布在  iOS
关注(0)|答案(2)|浏览(129)

我正在为iOS 14开发一个SwiftUI应用程序,我有一个侧边栏列表,它使用list的新children:属性使列表可扩展:

然而,目前我只能在点击显示箭头时展开这个列表。我希望在点击“我的酷项目”文本时也能展开这个列表。
这在“文件”应用程序中是可能的-当我点击“位置”文本时,我可以看到“位置”项的所有子项,但我不知道如何在我的应用程序中做到这一点。

为了澄清,每个项目的List项是Text视图,子列表项是NavigationLink
这种行为在SwiftUI中是否可能,甚至通过onTapGesture编程或使用List以外的其他工具?提前感谢您的帮助/建议!

kx5bkwkv

kx5bkwkv1#

这是一个方法的演示(当然,在您的项目中,您会将展开/折叠状态移动到视图模型中)

struct DemoDisclosureGroups: View {
    let items: [Bookmark] = [.example1, .example2, .example3]
    @State private var flags: [Bool] = [false, false, false]

    var body: some View {
        List {
            ForEach(Array(items.enumerated()), id: \.1.id) { i, group in
                DisclosureGroup(isExpanded: $flags[i]) {
                    ForEach(group.items ?? []) { item in
                        Label(item.name, systemImage: item.icon)
                    }
                } label: {
                    Label(group.name, systemImage: group.icon)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            withAnimation {
                                self.flags[i].toggle()
                            }
                        }
                }
            }
        }
    }
}

struct Bookmark: Identifiable {
    let id = UUID()
    let name: String
    let icon: String
    var items: [Bookmark]?

    // some example websites
    static let apple = Bookmark(name: "Apple", icon: "1.circle")
    static let bbc = Bookmark(name: "BBC", icon: "square.and.pencil")
    static let swift = Bookmark(name: "Swift", icon: "bolt.fill")
    static let twitter = Bookmark(name: "Twitter", icon: "mic")

    // some example groups
    static let example1 = Bookmark(name: "Favorites", icon: "star", items: [Bookmark.apple, Bookmark.bbc, Bookmark.swift, Bookmark.twitter])
    static let example2 = Bookmark(name: "Recent", icon: "timer", items: [Bookmark.apple, Bookmark.bbc, Bookmark.swift, Bookmark.twitter])
    static let example3 = Bookmark(name: "Recommended", icon: "hand.thumbsup", items: [Bookmark.apple, Bookmark.bbc, Bookmark.swift, Bookmark.twitter])
}
flvlnr44

flvlnr442#

您也可以使用背景色和.onTapGesture,如以下代码所示:

struct DisclosureGroupNew: View {

@State var show = false
@State var selectedCountry = ""

var body: some View {
    VStack {
        DisclosureGroup(isExpanded: $show) {
            ScrollView {
                LazyVStack(alignment: .leading, spacing: 15) {
                    ForEach(0...20, id: \.self) { index in
                        Text("Country \(index)")
                            .onTapGesture {
                                selectedCountry = "Country \(index)"
                                withAnimation {
                                    show.toggle()
                                }
                            }
                    }
                }
            }
            .padding(.top)
            .frame(height: 150) // if you need a fixed size

        } label: {
            Text("Label")
        }
        .padding()
        .background(Color.black.opacity(0.06).onTapGesture {
            show.toggle()
        })
        .cornerRadius(12)
        .padding()
        
        Text("Selected country: \(selectedCountry)")
        Spacer()
    }
}}

相关问题