将文本固定到滚动视图 SwiftUI 的底部

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

如何将视图元素固定到 SwiftUI 中滚动视图/VStack 的底部?我尝试过使用垫片,但这似乎不起作用。我需要将“页脚”文本放置在滚动视图/Vstack 的底部,我该怎么做?

struct ContentView: View {
    var body: some View {
        VStack {
            ScrollView(.vertical, showsIndicators: false) {
                VStack(alignment: .leading) {
                    Text("Top Title")
                    Text("Body")
                    Spacer()
                    Text("Footer") // SHOULD BE PINNED TO BOTTOM OF SCROLL VIEW
                        .frame(alignment: .bottom)
                }
            }
            .background(Color.red)

            Button("Done") {
                //some action
            }
        }
    }
}

swift swiftui scrollview vstack
2个回答
6
投票

这是可能的方法:

ScrollView(.vertical, showsIndicators: false) {
    VStack(alignment: .leading) {
        Text("Top Title")
        Text("Body")
    }
}
.overlay(
    Text("Footer") 
, alignment: .bottom)   // << here !!

猜测2:可能你想要这个

GeometryReader { gp in
    ScrollView(.vertical, showsIndicators: false) {
        VStack(alignment: .leading) {
            Text("Top Title")
            Text("Body")
            Spacer()
            Text("Footer") // SHOULD BE PINNED TO BOTTOM OF SCROLL VIEW
                .frame(alignment: .bottom)
        }
        .frame(maxWidth: .infinity, minHeight: gp.size.height)
    }
    .background(Color.red)
}

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