SwiftUI 列表在底部添加空 space
SwiftUI List add empty space at the bottom
我想让 SwiftUI List
一直滚动到最后一个 Section
的 header 将位于 [=14= 顶部的位置] 看法。我知道我可以使用 GeometryReader
来计算 List
的高度,并且通过了解每个列表单元格的 height
我可以计算出我应该添加多少空 space列表底部将其向上推。问题是,如果单元格会扩展或具有灵活的大小,那么它将不起作用。我想知道是否有一些我不知道的修饰符或一些更“灵活”的方法来做到这一点?
这是 List
的代码
import SwiftUI
struct ListSections: View {
var body: some View {
List {
Section(header: Text("Header 1")) {
ForEach((0...10), id: \.self) { index in
Text("item \(index)")
}
}
Section(header: Text("Header 2")) {
ForEach((0...12), id: \.self) { index in
Text("item \(index)")
}
}
}
}
}
在下图中,您可以在左侧看到默认情况下我可以滚动多远,在右侧我希望能够滚动多远。
这里有两个选项:
选项 1(首选方式)
只需在最后一部分添加底部填充:
struct ListSections: View {
var body: some View {
List {
Section(header: Text("Header 1")) {
ForEach((0...10), id: \.self) { index in
Text("item \(index)")
}
}
Section(header: Text("Header 2")) {
ForEach((0...12), id: \.self) { index in
Text("item \(index)")
}
}
.padding(.bottom, 300)
}
}
}
选项 2:
在 List 的最后一部分之后添加一个 View 就可以了。例如,我使用了颜色视图。
您的代码应如下所示:
struct ListSections: View {
var body: some View {
List {
Section(header: Text("Header 1")) {
ForEach((0...10), id: \.self) { index in
Text("item \(index)")
}
}
Section(header: Text("Header 2")) {
ForEach((0...12), id: \.self) { index in
Text("item \(index)")
}
}
Color(.clear)
.frame(height: 300)
}
}
}
我想让 SwiftUI List
一直滚动到最后一个 Section
的 header 将位于 [=14= 顶部的位置] 看法。我知道我可以使用 GeometryReader
来计算 List
的高度,并且通过了解每个列表单元格的 height
我可以计算出我应该添加多少空 space列表底部将其向上推。问题是,如果单元格会扩展或具有灵活的大小,那么它将不起作用。我想知道是否有一些我不知道的修饰符或一些更“灵活”的方法来做到这一点?
这是 List
import SwiftUI
struct ListSections: View {
var body: some View {
List {
Section(header: Text("Header 1")) {
ForEach((0...10), id: \.self) { index in
Text("item \(index)")
}
}
Section(header: Text("Header 2")) {
ForEach((0...12), id: \.self) { index in
Text("item \(index)")
}
}
}
}
}
在下图中,您可以在左侧看到默认情况下我可以滚动多远,在右侧我希望能够滚动多远。
这里有两个选项: 选项 1(首选方式) 只需在最后一部分添加底部填充:
struct ListSections: View {
var body: some View {
List {
Section(header: Text("Header 1")) {
ForEach((0...10), id: \.self) { index in
Text("item \(index)")
}
}
Section(header: Text("Header 2")) {
ForEach((0...12), id: \.self) { index in
Text("item \(index)")
}
}
.padding(.bottom, 300)
}
}
}
选项 2: 在 List 的最后一部分之后添加一个 View 就可以了。例如,我使用了颜色视图。
您的代码应如下所示:
struct ListSections: View {
var body: some View {
List {
Section(header: Text("Header 1")) {
ForEach((0...10), id: \.self) { index in
Text("item \(index)")
}
}
Section(header: Text("Header 2")) {
ForEach((0...12), id: \.self) { index in
Text("item \(index)")
}
}
Color(.clear)
.frame(height: 300)
}
}
}