简体   繁体   English

如何在 Swiftui 的主视图底部的滚动视图中放置非粘性按钮? Spacer 在滚动视图中似乎没有效果

[英]How to place a non-sticky button inside a scrollview to the bottom of a main view in Swiftui? Spacer seems to have no effect inside a scrollview

I am trying to place a button inside a scrollview to the bottom of the main view with some bottom padding after a spacer.我试图在滚动视图中放置一个按钮到主视图的底部,并在间隔后添加一些底部填充。 But looks like there is no effect of spacer inside a scrollview.但看起来滚动视图内的间隔没有效果。 I don't want to hardcode spacer with minWidth.我不想用 minWidth 硬编码间隔。 Any suggestion would be appreciated?任何建议将不胜感激?

 ScrollView {
        VStack {
            Spacer()
            NavigationLink(destination: Text("Hi")) {
                Button {
                } label: {
                    Text("Submit")
                        .frame(height: 42)
                }
                .buttonStyle(FilledButton())
                .frame(width: 248, height: 42, alignment: .center)
            }
            .padding(.top, 33)
            .padding(.bottom, 12)
        }
    }

I believe you need a Geometry Reader to grab the height of the ScrollView itself.我相信您需要一个几何阅读器来获取 ScrollView 本身的高度。

struct ContentView: View {
    var body: some View {
        GeometryReader { geometry in
            ScrollView {
                VStack {
                    Spacer()
                    NavigationLink(destination: Text("Hi")) {
                        Button {
                        } label: {
                            Text("Submit")
                                .frame(height: 42)
                        }
                        .buttonStyle(FilledButton())
                        .frame(width: 248, height: 42, alignment: .center)
                    }
                    .padding(.top, 33)
                    .padding(.bottom, 12)
                }
                .frame(maxWidth: .infinity)
                .frame(height: geometry.size.height)
            }
        }
    }
}

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

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