- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do you hide the onscreen keyboard in iOS App?
To hide a keyboard on screen we need to make use of some internal functions that are predefined in the iOS SDK. The keyboard appears on screen when we are typing in a text Field or textView. We need to make use of internal function according to the text field.
For example if the current text field is tfOne, we can hide the text field using the code below:
tfOne.resignFirstResponder()
This code will hide the keyboard when ever called, we may call this on an action for a button or for gesture recognizer.
This method is good for limited textFields, but we need to make it better if we want to achieve the same with multiple textFields or textviews.
We can create a function for the same.
func hideKeyboardWhenTappedAround() { let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard)) tap.cancelsTouchesInView = false view.addGestureRecognizer(tap) } @objc func dismissKeyboard() { view.endEditing(true) }
We can use this function in our class and it will hide the keyboard whenever we tap on the screen outside any text field or text view.
But as you can see that it needs a view on which the gesture will be added, hence we need to embed this function into an UIViewExtension.
extension UIViewController { func hideKeyboardWhenTappedAround() { let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard)) tap.cancelsTouchesInView = false view.addGestureRecognizer(tap) } @objc func dismissKeyboard() { view.endEditing(true) } }
Now we can call this function in our viewDidLoad() and then whenever we tap anywhere in the view except a text View/ Field, the keyboard will be hidden.
- Related Articles
- How to hide the status bar in a iOS App using Swift?
- How do I programmatically “restart” an iOS app?
- How to close or hide the virtual keyboard on Android?
- Best CRMs for iPhone: Which iOs app has the features you need?
- How to integrate Emojis Keyboard in an Android app?
- How to use microsoft keyboard app word flow
- How to check notifications status for the iOS App
- How to create CollectionView Layout in an iOS App?
- How to hide soft KeyBoard on android after clicking outside edittext?
- How to get the dimensions of a view in iOS App?
- How to create scrollable TextView on iOS App?
- How to create Tab Bar Layout in an iOS App?
- How do you animate the change of background color of a view on iOS?
- How to get the build/version number of an iOS App?
- Launching the App Store from an iOS application
