Search code examples
iosiphoneswiftuikeyboardswift-keyboard

In SwiftUI, how do I remove a view stuck on top of the keyboard


I have a view under a Form. When I enter a text field the view is stuck on top of the keyboard.

Code in question:

  // other stuff

  Form {
    Section {
      TextField("Enter your desired username", text: $page.username)
    }
    
    Section {
      Button(action: createUser) {
        Label("Sign Up", systemImage: "person.crop.circle.badge.plus")
      }
    }
  }
  
  // this is getting stuck on top of keyboard
  Group {
    Text("By signing up you agree to")
    // other stuff
  }

What it looks like:

Bug in question

As you can see, the "By signing up you..." view is stuck on top of the keyboard. I suspect it has something to do with Menu.

How do I get rid of it?


Solution

  • Do you have the Form and Group inside some other container, like a VStack? I assume you must… if so, the solution is to add .ignoresSafeArea(.keyboard, edges: .bottom) to that container.

    e.g.,

    VStack {
        Form {
            ...
        }
        Group {
            ...
        }
    }
    .ignoresSafeArea(.keyboard, edges: .bottom)
    

    Applying ignoresSafeArea to the Group doesn’t work because its container (here a VStack) is still resized by the keyboard. If adding ignoresSafeArea to your container has undesirable consequences, please post more of your code so we can understand the situation.