ios – How to stop SwiftUI from moving TextField up when the keyboard is opened?


I have a TextField near the top of the screen, followed by a spacer and more content below. The TextField moves outside of the screen when the keyboard is opened.

I’ve tried adding .ignoresSafeArea(.keyboard), separating the TextField from the rest of the content into two VStacks nestled in a ZStack, and a few other things found online, but nothing seems to do the trick so far.

Example:

enter image description here

Code to reproduce:

struct ContentView: View {
    @State private var text = ""
    
    var body: some View {
        VStack {
            TextField("Text", text: $text)
                .font(.largeTitle.weight(.medium))
                .padding()
            
            Spacer()
            
            VStack {
                Text("Content")
                Text("Content")
                Text("Content")
                Text("Content")
                Text("Content")
                Text("Content")
                Text("Content")
                Text("Content")
                Text("Content")
                Text("Content")
            }
            .padding()
            
            VStack {
                Text("Content")
                Text("Content")
                Text("Content")
                Text("Content")
                Text("Content")
                Text("Content")
                Text("Content")
                Text("Content")
                Text("Content")
                Text("Content")
            }
            .padding()
        }
        .ignoresSafeArea(.keyboard)
    }
}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img