I find that using resignFirstResponder
doesn't animate keyboard dismissal.
I wonder why, any ideas?
Here's a simple example:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
let textField = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .system)
button.setTitle("Dismiss", for: .normal)
button.addTarget(self, action: #selector(pressed), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(button)
view.addConstraints([
.init(item: button, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0),
.init(item: button, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 100),
])
textField.backgroundColor = .gray
textField.delegate = self
textField.placeholder = "Type here"
textField.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(textField)
view.addConstraints([
.init(item: textField, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0),
.init(item: textField, attribute: .top, relatedBy: .equal, toItem: button, attribute: .top, multiplier: 1, constant: 100),
.init(item: textField, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 200),
.init(item: textField, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 50),
])
}
@objc private func pressed() {
textField.resignFirstResponder()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}