How to Dismiss Keyboard iOS Programmatically When Pressing Return

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 from all view hierarchy programmatically in swift

If you're trying to dismiss keyboard in UIViewController try:

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

Dismiss keyboard through return key with swift

Try setting the delegate. Add a didSet to the text outlet. Like this.

    @IBOutlet var text: UITextField! {
didSet {
text.delegate = self
}
}

Dismissing the keyboard when a button is pressed, programmatically with swift?

It all depends on what triggered the keyboard to appear in the first place. If you have a UITextField on your page that brought it up, you can call textField.resignFirstResponder() to hide the keyboard again.

If you used some other object to bring up the keyboard, simply take the reference of whatever you used and call resignFirstResponder() on that object.

Example:

Lets say you are using a button button1 to close the keyboard, and you have a textField1 that is triggering the keyboard.

button1.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "buttonTapped"))

Then in your buttonTappedFunction

func buttonTapped(){
textField1.resignFirstResponder()
}

How do i dismiss the keyboard on return key from a UITextView

Remember to add the UITextDelegate into ViewDidLoad()

 func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if text == "\n"
{
textView.resignFirstResponder()
return false
}
return true
}

Hide keyboard by pressing button

[self.view endEditing:YES]

Paste it in action handler of your controller.
Or for Swift:

self.view.endEditing(true)

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



Related Topics



Leave a reply



Submit