I have created a signup form with a UIAlertController
and used the method addTextFieldWithConfigurationHandler
to add a text field. But there is a little problem.
When the form shows up, the keyboard and modal appear with a smooth animation. When closing the form, the modal disappears first, and then the keyboard disappears. This makes the keyboard make a sudden downward fall.
How can I make the modal and keyboard graciously disappear?
lazy var alertController: UIAlertController = { [weak self] in
let alert = UIAlertController(title: "Alert", message: "This is a demo alert", preferredStyle: .Alert)
alert.addTextFieldWithConfigurationHandler { textField in
textField.delegate = self
}
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
return alert
}()
@IBAction func alert() {
presentViewController(alertController, animated: true, completion: nil)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
alertController.dismissViewControllerAnimated(true, completion: nil)
return true
}
Its pretty simple.
if your
UIAlertController
delegate are present in self View Controller. then you can do it in its delegate method for Dismiss AlertController. You can[youtTextField resignFirstResponder]
in your UIAlertController object which have a button for dismiss it. (like OK or Cancel) so your presented KeyBoard will be dismissed.I didn't tried it but It will work. but you have to handle textField and Alert correctly.
You can set your view controller or other object as transitioning delegate of your UIAlertController (
alert.transitioningDelegate
) and make a custom animation for dismissing. Code sample:Result:
P.S.: I used old alert's transitioning delegate for presentation because I can't reproduce an original animation. So
animatePresentation:
method is never used.