【问题标题】:SwiftUI (dismiss-keyboard) overlay disables underlying actionsSwiftUI (dismiss-keyboard) 覆盖禁用底层操作
【发布时间】:2021-04-25 18:15:04
【问题描述】:

此问题与How to hide keyboard when using SwiftUI?有关

示例代码:

struct ContentView: View {
    @State var text = ""
    
    var body: some View {
        Background {
            NavigationView {
                List {
                    TextField("TextInput", text: $text)
                    Text("OnTapGesture")
                        .onTapGesture {
                            print("Text Tapped") //works
                        }
                    Button("Tap me") {
                        print("Btn Tapped") //doesn't work
                    }
                    NavigationLink("Go to detail", destination: Text("DetailView")) //doesn't work
                }
            }
        }
    }
}

struct Background<Content: View>: View {
    private var content: Content
    
    init(@ViewBuilder content: @escaping () -> Content) {
        self.content = content()
    }
    
    var body: some View {
        Rectangle()
            .overlay(content)
            .onTapGesture {
                self.endEditing()
            }
    }
    
    func endEditing() {
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}

这里的覆盖按预期工作,我可以点击任意位置来关闭键盘。但是,通过在叠加层上添加onTapGesture,某些其他操作不再起作用。这是故意的吗?它是一个错误吗?有没有办法解决这个问题?

【问题讨论】:

    标签: ios swift swiftui keyboard uiresponder


    【解决方案1】:

    找到了一个单一的解决方案,灵感来自SwiftUI: How to dismiss the keyboard by tap on NavigationLink?

    .gesture(DragGesture(minimumDistance: 20, coordinateSpace: .local).onChanged{_ in endEditing() })
    

    这将使所有操作保持启用(包括删除手势)并在滚动时关闭键盘...

    【讨论】:

      【解决方案2】:

      将此修饰符添加到按钮

      Button(action: {}) {
          Text("Button Tap")
          }
          .buttonStyle(PlainButtonStyle())
      

      完整代码

      struct ContentView: View {
      
      @State var text = ""
      @State private var tap: Bool = false
      
      var body: some View {
          Background {
              NavigationView {
                  
                  List {
                      
                      TextField("TextInput", text: $text)
                      
                      Text("OnTapGesture")
                          .onTapGesture {
                              print("Text Tapped") //works
                          }
                      
                      Button(action: buttonTapped) {
                          Text("Button Tapped")
                      }
                      .buttonStyle(PlainButtonStyle())
                      
                      NavigationLink(destination: Text("DetialView"), isActive: $tap) {
                          Button(action: tapButton) {
                              Text("Go to Navigation")
                          }
                          .buttonStyle(PlainButtonStyle())
                      }
                  }
              }
          }
      }
      
      func buttonTapped() {
          print("Button Tapped...")
      }
      
      func tapButton() {
          tap.toggle()
      }
      

      }

      希望一切顺利

      【讨论】:

      • 我不想更改按钮样式。另外,它对NavigationLink 没有任何影响。
      猜你喜欢
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-04
      • 1970-01-01
      相关资源
      最近更新 更多