9

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?

jaz01
  • 111
  • 1
  • 1
  • 8
  • nope in a sense that i made a sign up page and once i tap on a textfield it pushes the view up – jaz01 Apr 15 '21 at 09:36
  • i just had seen online that only way of resolving this is including a scroll view since the ignoresSafeArea doesn't work with a VStack – jaz01 Apr 15 '21 at 09:46
  • can you help @Andrew – jaz01 Apr 15 '21 at 10:00
  • @jnpdx could you help me please – jaz01 Apr 15 '21 at 10:00
  • @jaz01 as has been pointed out, this question has been answered before on SO. If the existing solution doesn't work for you, you would need to show the code you've tried and explain why your situation is different and how the existing solution failed to work. – jnpdx Apr 15 '21 at 19:05
  • @jaz01 Did you find solution for it? I am also facing same issue – Beena Aug 24 '22 at 12:31

1 Answers1

23

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)
  }
}
Maksymilian Tomczyk
  • 1,100
  • 8
  • 16
  • but is GeometryReader{} really needed @MaksymilianTomczyk – jaz01 Apr 15 '21 at 13:57
  • Good point, GeometryReader is leftover from another experiment. Editing answer to remove GeometryReader. – Maksymilian Tomczyk Apr 15 '21 at 14:09
  • but i tired that and it didn't work for me. @Maksymilian Tomczyk – jaz01 Apr 15 '21 at 18:50
  • I tried running it without GeometryReader on empty project and it worked. Is your content wrapped in some other containers? – Maksymilian Tomczyk Apr 16 '21 at 03:02
  • i will update the question with my code for you to have a look. – jaz01 Apr 16 '21 at 09:11
  • this is not working either – Akhtar Jun 22 '22 at 20:44
  • I'm having quite a strange behavior. This works everywhere besides a Sidebar in a `NavigationSplitView`. In the Sidebar the view with your setup moves slightly *down* like 10-20 pts when the keyboard appears. I have 3 empty state views that might get triggered simultaenously on an iPad in Landscape and this move down only happens in the Sidebar regardless of which EmptyStateView I use there. Maybe a bug :) – David Mar 12 '23 at 19:39