[英]SwiftUI: How to get scrollview to contain full list length
我有一個滾動視圖,其中包含一個包含一些文本和列表的 vstack。 列表獨立於滾動視圖滾動,導致文本成為列表標題的效果。 我想讓滾動視圖而不是讓列表完全展開,以便所有內容一起滾動。
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()
}
您實際上已經在此處獲得了兩個 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)
}
}
}
}
這是帶標題和不帶標題的結果:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.