I have little experience in Objective-C. I want to hide the keyboard for a text field using the Swift programming language.
I also tried this
func te
Just override the UIViewController method called "touchesBegan" and set endEditing to true. Just like this:
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
self.view.endEditing(true)
}
First you need to set delegate
for your textfield then you need to include resignFirstResponder()
to hide keyboard when press return button of keybaord.
func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore.
{
textField .resignFirstResponder()
return true;
}
You can try this code to have the "return" key execute code.
func textFieldShouldReturn(textField: UITextField) -> Bool{
textField.resignFirstResponder()
performAction()
return true;
}
func performAction(){
//execute code for your action inside this function
}
Hope this can help you.
UIApplication.sharedApplication().sendAction(#selector(UIResponder.resignFirstResponder), to:nil, from:nil, forEvent:nil)
when you call your UIAlertController, place condition in the completion handler if you'd like to hide the keyboard.
self.present('nameOfYourUIAlertController', animated: true, completion: {
if condition == true {
'nameOfYourUIAlertController'.textFields![0].resignFirstResponder()
}
})
This works great for me.
In order to hide the keyboard when you pressed a button, you need to use the following function.
self.view.endEditing(true)
This works when a button is pressed.
Hope this helps someone out there.