Why in iOS14 (or at least iOS14.4.1 iPhone X) keyboardWillShowNotification is fired when background(UIScrollerView) taped, while in iOS13 is not?
I am trying to run a function when the keyboard shows and disappears, have the following code:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var textField: UITextField!
let notificationCenter = NotificationCenter.default
override func viewDidLoad() {
super.viewDidLoad()
notificationCenter.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
deinit {
notificationCenter.removeObserver(self)
}
@objc private func keyboardWillShow(_ notification: Notification) { updateBottomLayoutConstraintWithNotification(notification: notification, bottomConstraint: nil, tag: "keyboardWillShow()")
}
@objc private func keyboardWillHide(_ notification: Notification) { updateBottomLayoutConstraintWithNotification(notification: notification, bottomConstraint: nil, tag: "keyboardWillHide()")
}
func updateBottomLayoutConstraintWithNotification(notification: Notification, bottomConstraint: NSLayoutConstraint?, tag: String = "") {
print("[Caller Name] \(tag)")
}
}
Operation steps
- Tap the textField(Keyboard Raised)
- Tap the background(UIScrollerView)
Such a log will be generated
[Caller Name] keyboardWillShow()
[Caller Name] keyboardWillShow()
Thank you for your attention!