gpt4 book ai didi

swiftui - 当嵌入到 NavigationView SwiftUI 时,使列表部分在 SwiftUI 中不可折叠

转载 作者:行者123 更新时间:2023-12-04 02:30:46 24 4
gpt4 key购买 nike

当我将分组到 List 中的 Section 嵌入到 NavigationView 中时,部分标题变得可折叠。我想让它们不可折叠,就像 List 没有嵌入到 NavigationView 中一样。
我当前的代码(使用 NavigationView):

import SwiftUI

struct MyGroup {
var name:String, items:[String]
}

struct ContentView: View {
var groups : [MyGroup] = [
.init(name: "Animals", items: ["🐕","🐩","🐂","🐄","🐈","🦩","🐿","🐇"]),
.init(name: "Vehicles", items: ["🚕","🚗","🚃","🚂","🚟","🚤","🛥","⛵️"])]

var body: some View {
NavigationView {
VStack {
List {
ForEach(groups, id: \.self.name) { group in
Section(header: Text(group.name)) {
ForEach(group.items, id:\.self) { item in
Text(item)
}
}
}
}
}.navigationTitle("collections")
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
list with collapsible sections

最佳答案

它是应用的默认样式,您可以像下面这样为 List 显式设置它(使用 Xcode 12/iOS 14 测试)
demo

    List {
ForEach(groups, id: \.self.name) { group in
Section(header: Text(group.name)) {
ForEach(group.items, id:\.self) { item in
Text(item)
}
}
}
}.listStyle(InsetGroupedListStyle()) // or GroupedListStyle

关于swiftui - 当嵌入到 NavigationView SwiftUI 时,使列表部分在 SwiftUI 中不可折叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64350979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com