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

Viewed 14316

I am experimenting with SwiftUI and just trying to have a button at the bottom. Right now it is centered. Wondering how I could force a view to stick superview's bottom as you would do in AutoLayout.

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

Thank you!!!

2 Answers

You have to add a Spacer view above the text.

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

You can use Scrollview to make your long content to not take header and footer place. Like this code:

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