【发布时间】:2022-10-20 19:11:02
【问题描述】:
我正在 SwiftUI 中为 iOS 制作一个登录界面。用户应该能够通过点击软件键盘上的“下一步”按钮轻松地从用户名文本字段切换到密码文本字段。它运行良好,但由于某种原因在两个文本字段之间切换时键盘总是弹跳一点。编辑:正如this answer 中所建议的那样,我在 VStack 中添加了一个 Spacer 以使其填充可用空间。文本字段不再弹跳,但不幸的是键盘仍然弹跳。我更新了代码和 GIF 以反映我的更改。
谷歌搜索了一下,这似乎不是一个很常见的问题。 This question 似乎与发生在我身上的事情相似,但是按照答案并将文本字段包装在 ScrollView 或 GeometryReader 中并没有改变任何东西。这是我的代码:
struct AuthenticationView: View {
@State var userName: String = ""
@State var userAuth: String = ""
@FocusState var currentFocus: FocusObject?
enum FocusObject: Hashable { case name, auth }
var body: some View {
VStack(spacing: 8) {
TextField("Username", text: $userName)
.focused($currentFocus, equals: .name)
.padding(8).background(Color.lightGray)
.cornerRadius(8).padding(.bottom, 8)
.textInputAutocapitalization(.never)
.onSubmit { currentFocus = .auth }
.autocorrectionDisabled(true)
.keyboardType(.asciiCapable)
.textContentType(.username)
.submitLabel(.next)
SecureField("Password", text: $userAuth)
.focused($currentFocus, equals: .auth)
.padding(8).background(Color.lightGray)
.cornerRadius(8).padding(.bottom, 16)
.textInputAutocapitalization(.never)
.onSubmit { currentFocus = nil }
.autocorrectionDisabled(true)
.keyboardType(.asciiCapable)
.textContentType(.password)
.submitLabel(.done)
Spacer() // This fixes the text fields
// But it does not fix the keyboard
}.padding(32)
}
}
【问题讨论】: