Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stick button at bottom when keyboard appears

Tags:

ios

swiftui

I'm making a screen when I ask the user his name and a Button to the bottom of the page. My problem is, when I'm focusing the Textfield, the keyboard appears but push up the button. How can I stick my button to the bottom of my view and be hidden by the keyboard ?

Before the keyboard appears: Before the keyboard appears:

When the keyboard showed up: When the keyboard showed up:

Thanks a lot !

like image 433
Vinestro Avatar asked Dec 18 '25 21:12

Vinestro


1 Answers

You can use .ignoresSafeArea(.keyboard) modifier:

struct ContentView: View {
    @State var text = "Test"
    var body: some View{
        VStack {
            Text("Hello, world")
            Spacer()
            TextField("", text: $text)
            Spacer()
            Button("Submit") {}
        }.ignoresSafeArea(.keyboard)
    }
}

This has to be applied to the surrounding parent stack -- applying it to the Button element alone in the above example has no effect.

like image 72
jnpdx Avatar answered Dec 20 '25 11:12

jnpdx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!