Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

swiftui

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?

like image 991
jaz01 Avatar asked Dec 21 '25 19:12

jaz01


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)
  }
}
like image 59
Maksymilian Tomczyk Avatar answered Dec 24 '25 10:12

Maksymilian Tomczyk



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!