简体   繁体   English

SwiftUI 列表底部添加空格

[英]SwiftUI List add empty space at the bottom

I would like to allow the SwiftUI List to scroll all the way up to the place where header of the last Section will be at the top of the List view.我想让SwiftUI List一直滚动到最后一Section的 header 将位于List视图顶部的位置。 I know that I could use GeometryReader to calculate height of the List and by knowing the height of each list cell I could calculate how much empty space I should add at the bottom of the list to push it up.我知道我可以使用GeometryReader来计算Listheight ,我可以计算我应该在列表底部添加多少空白空间以将其向上推。 The problem is that if cells will for example expand or have flexible size, then it won't work.问题是,如果单元格例如会扩展或具有灵活的大小,那么它将不起作用。 I was wondering if there is maybe some modifier that I don't know about or some more "flexible" way to do that?我想知道是否有一些我不知道的修饰符或更“灵活”的方式来做到这一点?

This is the code for the List这是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)")
                }
            }
        }
    }
}

On the image below you can see on the left how far I can scroll by default and on the right how far I would like to be able to scroll.在下图中,您可以在左侧看到默认情况下我可以滚动多远,在右侧可以看到我希望能够滚动多远。

在此处输入图像描述

There are two options here: Option 1 (preferred way) Just adding a bottom padding to the last Section:这里有两个选项:选项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)
        }
    }
}

Option 2: Adding a View after the last section inside your List will do the trick.选项 2:在 List 的最后一部分之后添加一个 View 就可以了。 For instance I used a Color view.例如,我使用了颜色视图。

Your code should look like:您的代码应如下所示:

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)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM