简体   繁体   English

如何在 SwiftUI 中结束对 UIViewRepresentable 的编辑?

[英]How to endEditing on UIViewRepresentable in SwiftUI?

I have a first responder textField but I want to be able to close the keyboard when the screen is tapped or when the user presses a button.我有一个第一响应者textField但我希望能够在点击屏幕或用户按下按钮时关闭键盘。

Here is my UIViewRepresentable :这是我的UIViewRepresentable

public struct CustomSTPPaymentCardTextField: UIViewRepresentable {

@Binding var paymentMethodParams: STPPaymentMethodParams?
let background: Color = ColorManager.backgroundColor

public init(paymentMethodParams: Binding<STPPaymentMethodParams?>) {
    _paymentMethodParams = paymentMethodParams
    
    
}

public func makeCoordinator() -> Coordinator {
    return Coordinator(parent: self)
}

public func makeUIView(context: Context) -> STPPaymentCardTextField {
    let paymentCardField = STPPaymentCardTextField()
    paymentCardField.borderColor = nil
    paymentCardField.borderWidth = 0

    paymentCardField.becomeFirstResponder()
   

    return paymentCardField
}


public func updateUIView(_ paymentCardField: STPPaymentCardTextField, context: Context) {
    
}

public class Coordinator: NSObject, STPPaymentCardTextFieldDelegate {
    var parent: CustomSTPPaymentCardTextField
    init(parent: CustomSTPPaymentCardTextField) {
        self.parent = parent
    }

}

} }

Here is how I called it in the view:这是我在视图中的调用方式:

CustomSTPPaymentCardTextField(paymentMethodParams: $paymentMethodParams)

I've tried to pass a binding boolean to activate endEditing我试图通过绑定 boolean 来激活endEditing

I've also tried to use the following function:我还尝试使用以下 function:

#if canImport(UIKit)
extension View {
    func hideKeyboard() {
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}
#endif

I've also tried the following:我还尝试了以下方法:

UIApplication.shared.resignFirstResponder()

I've tried all of the above methods with and without DispatchQueue.main.async but none of them seem to work.我已经尝试过使用和不使用DispatchQueue.main.async的所有上述方法,但它们似乎都不起作用。

Any help is appreciated: :)任何帮助表示赞赏::)

You're almost correct with the extension on View您对Viewextension几乎是正确的

Use endEditing on all the windows.在所有 windows 上使用endEditing That can be called from anywhere.可以从任何地方调用。

extension View {
  static func endEditing() {
    UIApplication.shared.windows.forEach { $0.endEditing(false) }
  }
}

That approach might not be correct if you had a multi-scene app.如果您有一个多场景应用程序,这种方法可能不正确。

Add an action to any button or in viewWillDisappear method inside UIViewController class在 UIViewController class 内的任何按钮或 viewWillDisappear 方法中添加操作

self.view.endEditing(true)

add this line.添加这一行。 This will dismiss keyboard and stops editing.这将关闭键盘并停止编辑。

It Worked fine for me.它对我来说很好。

#if canImport(UIKit)
extension View {
    func hideKeyboard() {
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 UIViewRepresentable SwiftUI 中重新加载 collectionview - How to reload collectionview in UIViewRepresentable SwiftUI SwiftUI:如何调整 UIViewRepresentable output 的大小? - SwiftUI: how to resize the UIViewRepresentable output? SwiftUI + UIViewRepresentable:如何让 UIViewRepresentable 响应绑定 - SwiftUI + UIViewRepresentable: how would you have the UIViewRepresentable respond to a binding 如何在 SwiftUI 中的 UIViewRepresentable 中更改 UITextField 大小 - How to change UITextField size inside UIViewRepresentable in SwiftUI 如何使用 SwiftUI 中的 UIViewRepresentable 更改 UITextField 中的宽度? - How to change Width in a UITextField with UIViewRepresentable in SwiftUI? UIViewRepresentable 中 SwiftUI 中的 AVSampleBufferDisplayLayer - AVSampleBufferDisplayLayer in SwiftUI in UIViewRepresentable SwiftUI:UIViewRepresentable 小部件预览 - SwiftUI: UIViewRepresentable widget preview UITableView 与 UIViewRepresentable 在 SwiftUI - UITableView with UIViewRepresentable in SwiftUI 如何将 SwiftUI 视图放置在 UIKit (UIViewRepresentable) 视图之上? (错误或错误?) - How to place a SwiftUI view on top of a UIKit (UIViewRepresentable) view? (bug or mistake?) 在 UIViewRepresentable 和 SwiftUI 之间传递数据 - Pass data between UIViewRepresentable and SwiftUI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM