How we can get is keyboard is shown or hidden currently in our app?
Keyboard is shown
That is it. Simple solution, we just need to add observer for keyboard notification.
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardShow),
name: Notification.Name.UIKeyboardWillShow, object: nil)
}
@objc func handleKeyboardShow(notification: Notification) {
print("Keyboard will show")
}
This observer only works for event when keyboard will be shown, there is another observer for event when keyboard will be hidden.
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardShow),
name: Notification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardHide),
name: Notification.Name.UIKeyboardWillHide, object: nil)
}
@objc func handleKeyboardShow(notification: Notification) {
print("Keyboard will show")
}
@objc func handleKeyboardHide(notification: Notification) {
print("Keyboard will hide")
}
But there is a lot more thing beside this. And I want to tell you about some of them.