How to Dismiss the iOS Keyboard

How to dismiss keyboard iOS programmatically when pressing return

The simple way is to connect the delegate of UITextField to self (self.mytestField.delegate = self) and dismiss the keyboard in the method textFieldShouldReturn using [textField resignFirstResponder];

Another way to dismiss the keyboard is the following:

Objective-C

[self.view endEditing:YES];

Swift:

self.view.endEditing(true)

Put [self.view endEditing:YES]; where you would like to dismiss the keyboard (Button event, Touch event, etc.).

Dismiss keyboard in iOS

first of all write this extension in any swift file

extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)

}

func dismissKeyboard() {
view.endEditing(true)
}
}

Than in viewDidLoad of that View only call in any view controller there are textFields.

self.hideKeyboardWhenTappedAround()

Close iOS Keyboard by touching anywhere using Swift

override func viewDidLoad() {
super.viewDidLoad()

//Looks for single or multiple taps.
let tap = UITapGestureRecognizer(target: self, action: #selector(UIInputViewController.dismissKeyboard))

//Uncomment the line below if you want the tap not not interfere and cancel other interactions.
//tap.cancelsTouchesInView = false

view.addGestureRecognizer(tap)
}

//Calls this function when the tap is recognized.
@objc func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}

Here is another way to do this task if you are going to use this functionality in multiple UIViewControllers:

// Put this piece of code anywhere you like
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}

@objc func dismissKeyboard() {
view.endEditing(true)
}
}

Now in every UIViewController, all you have to do is call this function:

override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()
}

This function is included as a standard function in my repo which contains a lot of useful Swift Extensions like this one, check it out: https://github.com/goktugyil/EZSwiftExtensions

How can I pan down to dismiss keyboard?

ScrollView component has a property called keyboarddismissmode.

keyboardDismissMode


Determines whether the keyboard gets dismissed in response to a drag.

Cross platform

  • none (the default), drags do not dismiss the keyboard.
  • on-drag, the keyboard is dismissed when a drag begins.

iOS Only

  • interactive, the keyboard is dismissed interactively with the drag and moves in synchrony with the touch; dragging upwards cancels
    the dismissal. On android this is not supported and it will have the
    same behavior as 'none'.

I have never used this property but from the description and from this answer, I think this is what you are looking for.

How do I dismiss the iOS keyboard?

[myTextField resignFirstResponder]

Here, second paragraph in the Showing and Hiding the Keyboard section.

How do you dismiss the keyboard when editing a UITextField

I set the delegate of the UITextField to my ViewController class.

In that class I implemented this method as following:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}

UITextView - Keyboard Dismiss on Drag or Dismiss Interactively

For the built-in keyboard dismissing to work for a UIScollView or one of its subclasses (e.g. UITextView) the scroll view needs to be able to scroll. If there's not enough text in the text view to provide for scrolling, it won't perform a keyboard dismiss.

However, you can turn on vertical bouncing and then it will work. Check "Bounce Vertically" in interface builder or in code set myTextView.alwaysBounceVertical = true.

Dismiss keyboard after a user entered 11 digit

This is how it works

  func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

// YOU SHOULD FIRST CHECK FOR THE BACKSPACE. IF BACKSPACE IS PRESSED ALLOW IT

if string == "" {
return true
}

if let characterCount = textField.text?.count {
// CHECK FOR CHARACTER COUNT IN TEXT FIELD
if characterCount >= 11 {
// RESIGN FIRST RERSPONDER TO HIDE KEYBOARD
return textField.resignFirstResponder()
}
}
return true
}

EDIT

1) You should set the IBOutlet to your textField

2) Set delegate to self, on your textField.

3) YourViewController: UIViewController, UITextFieldDelegate { }

4) Implement the delegate method as in above. check for the backspace and allow if user enters backspace to remove characters from textField.



Related Topics



Leave a reply



Submit