the*_*bot 4 ios swiftui ipados ios14
我正在为 iOS 14 制作一个 SwiftUI 应用程序,我有一个侧边栏列表,它使用 list 的新children:
属性使列表可扩展:
但是,目前我只能在我精确单击披露箭头时展开此列表。例如,当我单击“我的酷项目”文本时,我也希望能够展开此列表。
这在“文件”应用程序中是可能的 - 当我单击“位置”文本时,我可以看到“位置”项的所有子项,但我不知道如何在我的应用程序中执行此操作。
为了澄清,List
每个项目的项目是一个Text
视图,子列表项目是NavigationLink
这种行为在 SwiftUI 中是否可能,甚至通过 onTapGesture 或除 a 以外的其他东西以编程方式进行List
?提前感谢您的任何帮助/建议!
Asp*_*eri 14
这是方法的演示(当然,在您的项目中,您会将展开/折叠状态移动到视图模型中)
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])
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2832 次 |
最近记录: |