swift - iOS 14 SwiftUI 键盘自动提升 View

标签 swift keyboard swiftui textfield ios14

我在我的 View 中使用 TextField,当它成为第一响应者时,它会离开 View ,如下面的 GIF 所示。
有什么办法可以摆脱这种行为吗?
enter image description here
这是我的代码

NavigationView(content: {
    ZStack{
        MyTabView(selectedIndex: self.$index)
            .view(item: self.item1) {
                NewView(title: "Hello1").navigationBarTitle("")
                    .navigationBarHidden(true)
            }
            .view(item: self.item2) {
                NewView(title: "Hello2").navigationBarTitle("")
                    .navigationBarHidden(true)
            }
            .view(item: self.item3) {
                NewView(title: "Hello3").navigationBarTitle("")
                    .navigationBarHidden(true)
            }
    }.navigationBarHidden(true)
    .navigationBarTitle("")
}).ignoresSafeArea(.keyboard, edges: .bottom)
//新 View
struct NewView:View {
    @State var text:String = ""
    var title:String
    var body: some View {
        VStack {
            Spacer()
            Text("Hello")
            TextField(title, text: self.$text)
                .textFieldStyle(RoundedBorderTextFieldStyle())
            
        }.padding()
        .onAppear {
            debugPrint("OnApper \(self.title)")
        }
    }
}

最佳答案

您应该在 ZStack 上应用修饰符, 不是 NavigationView

NavigationView(content: {
    ZStack{
        ,,,
    }.navigationBarHidden(true)
    .navigationBarTitle("")
    .ignoresSafeArea(.keyboard, edges: .bottom) // <- This line moved up
})

完整的工作示例:
struct ContentView: View {
    @State var text = ""
    var body: some View {
        VStack{
            Spacer()
            Text("Hello, World")
            TextField("Tap to test keyboard ignoring", text: $text)
                .textFieldStyle(RoundedBorderTextFieldStyle())
        }
        .padding()
        .ignoresSafeArea(.keyboard, edges: .bottom)
    }
}
Demo

关于swift - iOS 14 SwiftUI 键盘自动提升 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63958912/

相关文章:

javascript - 安卓浏览器: avoid keyboard to hide when icon is pressed

ios - Swift 应用程序在 iPad 上返回空白屏幕?

ios - SwiftUI 列表 - 禁用特定行的 move ?

ios - 插件工具包 : -[PKService run] iOS Keyboard extension error

SwiftUI SignInWithAppleButton 挂起

json - 快速解析 JSON

ios - 在 UIAlertController 中更改标题颜色

ios - 阴影不适用于顶部 Tableview 的圆角

swift - 为什么在 Swift 中使用自动生成的框架 header 会出现此错误?

安卓软键盘