Best way to dismiss Keyboard in a View Controller iOS (Swift)
Kaushal Elsewhere
99314

extension UIViewController {
 
 internal func hideKeyboardOnTap(){
 let tap = UITapGestureRecognizer(target: self, action: #selector(self.hideKeyboard))
 tap.cancelsTouchesInView = false
 self.view.addGestureRecognizer(tap);
 
 }
 
 @objc private func hideKeyboard(){
 self.view.endEditing(true);
 }
}

just call hideKeyboardOnTap() in any viewController viewDidLoad() function.

Best,

Like what you read? Give SDN SPR a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.