在 SwiftUI 中,如何删除卡在键盘顶部的视图

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

我在表格下有一个视图。当我输入文本字段时,视图卡在键盘顶部。

有问题的代码:

  // 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
  }

外观:

如您所见,“通过注册您...”视图停留在键盘顶部。我怀疑它与菜单有关。

我该如何摆脱它?

您是否在其他容器(如 VStack)中放置了 Form 和 Group?我假设您必须……如果是这样,解决方案是将 .ignoresSafeArea(.keyboard, edges: .bottom) 添加到该容器。

例如,

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

ignoresSafeArea 应用到 Group 不起作用,因为它的容器(此处为 VStack)仍由键盘调整大小。如果将 ignoresSafeArea 添加到您的容器会产生不良后果,请 post 您的更多代码,以便我们了解情况。