如何使用 SwiftUI 将按钮固定在视图底部?

How to stick my button to the bottom of the view using SwiftUI?

我正在尝试使用 SwiftUI 并尝试在底部添加一个按钮。现在它居中。想知道如何像在 AutoLayout 中那样强制视图粘在超级视图的底部。

struct ContentView : View {
    var body: some View {
        VStack {
            Text("Test")
        }
    }
}

谢谢!!!

您必须在文本上方添加 Spacer 视图。

struct ContentView : View {
    var body: some View {
        VStack {
            Spacer() // ←- here
            Text("Test")
        }
    }
}

您可以使用 Scrollview 让您的长内容不占据页眉和页脚的位置。喜欢这个代码:

struct ContentView : View {
    var body: some View {
        VStack {
            Group { // Let say this is your sticky header 
            }
            ScrollView(.vertical, showsIndicators: false) { 
                // This is your long content wrap in here 
            }
            Group { // And this is your sticky footer 
            }
        }
    }
}