简体   繁体   中英

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:

有问题的错误

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?

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.

eg,

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM