在 SwiftUI 中同时使用 SecureField 和 TextField 时键盘闪烁和屏幕跳转

Keyboard flickers and screen jumps when using SecureField and TextField together in SwiftUI

提问人:xen555 提问时间:11/7/2023 最后编辑:xen555 更新时间:11/7/2023 访问量:82

问:

这有效

struct TestView: View {
    @State private var email = ""
    @State private var password = ""

    var body: some View {
 
            VStack{
                TextField("Enter your email", text: $email)
                    .font(.subheadline)
                    .padding(12)
                    .background(Color(.systemGray6))
                    .cornerRadius(10)
                    .padding(.horizontal, 24)
                
                TextField("Enter your password", text: $password)
                    .font(.subheadline)
                    .padding(12)
                    .background(Color(.systemGray6))
                    .cornerRadius(10)
                    .padding(.horizontal, 24)
            }

        }
    }

仅限 TextFields

这会出现故障,导致屏幕和键盘闪烁/跳动

 struct TestView: View {
    @State private var email = ""
    @State private var password = ""

    var body: some View {
 
            VStack{
                TextField("Enter your email", text: $email)
                    .font(.subheadline)
                    .padding(12)
                    .background(Color(.systemGray6))
                    .cornerRadius(10)
                    .padding(.horizontal, 24)
                
                SecureField("Enter your password", text: $password)
                    .font(.subheadline)
                    .padding(12)
                    .background(Color(.systemGray6))
                    .cornerRadius(10)
                    .padding(.horizontal, 24)
            }

        }
    }


TextField + SecureField

我使用的是iOS17。我已经在模拟和物理(iPhone 13 Pro Max)设备上进行了尝试。它在物理设备上更明显,但我仍然在模拟器上注意到它。不确定这是我做错了什么,还是这是一个错误。

iOS Swift 调试 SwiftUI iOS17

评论

1赞 Sweeper 11/7/2023
我只在两个字段之间切换时看到它,我认为这是正常的(这似乎是由于自动键盘回避试图“滚动”您的视图引起的)。
0赞 workingdog support Ukraine 11/7/2023
无法重现您的问题。在我的测试中,在 MacOS 14.2、Xcode 15.1 上,在真实的 ios 17 设备(不是预览版)上测试,对我来说一切都很好。在较旧的系统上可能会有所不同。您是否有其他一些复杂的视图,例如 TabView 或工作表......?TestView
0赞 Cristik 11/7/2023
当故障发生时,我在控制台中看到一些自动布局警告,类似于 SwiftUI TextFields 中报告的 vanilla 全新项目给出错误 [LayoutConstraints] 的警告。如何解决?可能是 SwiftUI 错误。
0赞 someone 11/9/2023
这是一个错误。我在我一直在开发的多个应用程序中注意到了这一点。它是在 iOS 17 中引入的。问题似乎只出在SecureField上。

答: 暂无答案