如何在closures整个应用程序时closures键盘
我在popup键盘的不同viewController中有多个textFields。
我知道如何在用户点击屏幕的不同部分时closures键盘,但我不想去硬编码到我的应用程序的每个angular落。
那么,无论如何,当用户在键盘以外的任何地方点击屏幕时,强制键盘在应用程序的任何地方都会被解散。
我正在考虑扩展UIViewController,但我也有一些视图,我添加为子视图中的一些textFields。 也许有可能是我自己扩展TextField类?
我build议创build一个基本的 UIViewController
并让每个ViewControllerinheritance它; 在其中覆盖touchesBegan方法:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { view.endEditing(true) }
或者,您可以重写viewDidLoad(在基本ViewController中)并将UITapGestureRecognizer
添加到视图中,如下所示:
override func viewDidLoad() { let tapGesture = UITapGestureRecognizer(target: self, action: #selector(Base.endEditing)) view.addGestureRecognizer(tapGesture) } func endEditing() { view.endEditing(true) }
你也可以使用视图控制器的扩展,如果你想解除键盘适用于所有的人:
extension UIViewController { open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { self.view.endEditing(true) } }