SwiftUI 2.0 List with children - how to make the tappable area of the disclosure button cover the whole list item

后端 未结 1 675
一个人的身影
一个人的身影 2020-12-20 06:07

I am making a SwiftUI app for iOS 14, and I have a sidebar list that uses the new children: attribute of list to make the list expandable:

相关标签:
1条回答
  • 2020-12-20 06:24

    Here is a demo of approach (of course in your project you'd move expand/collapse state into view model)

    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])
    }
    
    0 讨论(0)
提交回复
热议问题