SwiftUI:如何获取滚动视图以包含完整列表长度

问题描述 投票:0回答:1

我有一个滚动视图,其中包含一个包含某些文本和列表的vstack。列表独立于滚动视图滚动,从而导致文本成为列表标题的效果。我想用scrollview代替,使列表完全展开,以便所有内容一起滚动。

ScrollView {
                VStack(alignment: .leading) {
                    Text(self.person.name)

                    Text(self.person.email)
                        .font(.subheadline)
                        .padding(.top)

                    Text(self.person.about)
                        .foregroundColor(.secondary)
                        .padding()

                    List {
                        Section {
                            ForEach(self.friends, id: \.id) { friend in
                                Text(friend.name)
                            }
                        }
                    }
                    Spacer()
                 }
                .padding()

            }

这是我在滚动列表时看到的:enter image description here

ios swift swiftui
1个回答
0
投票

您实际上在这里有两个ScrollView。您需要使用单个List并将其他项放置为Section标头。不幸的是,List似乎不支持整个视图的标头,但UITableView却支持。

    var body: some View {
        List {
            Section(header:
                VStack(alignment: .leading) {
                    Text(self.person.name)

                    Text(self.person.email)
                        .font(.subheadline)
                        .padding(.top)

                    Text(self.person.about)
                        .foregroundColor(.secondary)
                        .padding()
                }
            ) {
                ForEach(self.friends, id: \.id) { friend in
                    Text(friend.name)
                }
            }
        }
    }

编辑:也许您甚至没有在寻找标题?在这种情况下,只需将所需的内容放在ForEach上方即可。

    var body: some View {
        List() {
            Section {

                VStack(alignment: .leading) {
                    Text(self.person.name)

                    Text(self.person.email)
                        .font(.subheadline)
                        .padding(.top)

                    Text(self.person.about)
                        .foregroundColor(.secondary)
                        .padding()
                }

                ForEach(self.friends, id: \.id) { friend in
                    Text(friend.name)
                }
            }
        }
    }

这里是带有标题的结果,不包含:

enter image description here enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.