How to prevent keyboard from pushing up the view in SwiftUI?

Viewed 5142

i am new to SwiftUI programming, i've made a view but once the user taps on the textfield the keyboard appears and the entire view pushes up, how do i fix this?

1 Answers

I believe you can use .ignoresSafeArea(.keyboard)

I tested this code in XCode 12.4 and simulator iOS 14.4

struct ContentView: View {
  var body: some View {
      VStack {
        Spacer()
        Group {
          Text("Content")
          Text("Content")
          Text("Content")
          Text("Content")
          Text("Content")
          Text("Content")
          Text("Content")
          Text("Content")
          Text("Content")
        }
        HStack{
          Spacer()
          TextField("INPUT", text: .constant("HERE IS INPUT"))
          Spacer()
        }
        Group {
          Text("Content")
          Text("Content")
          Text("Content")
          Text("Content")
          Text("Content")
          Text("Content")
          Text("Content")
          Text("Content")
          Text("Content")
        }
        Spacer()
      }
      .ignoresSafeArea(.keyboard)
  }
}
Related