A wrapper around keyboard notifications.
- iOS 10.0+
- Xcode 11.0+
- Swift 5.0+
To integrate KeyboardHandler into your Xcode project with CocoaPods, specify it in your Podfile
:
pod 'Shakuro.KeyboardHandler'
Then, run the following command:
$ pod install
If you prefer not to use CocoaPods, you can integrate Shakuro.KeyboardHandler simply by copying it to your project.
Have a look at the KeyboardHandler_Example
Add a strong reference inside your UIViewController
:
private var keyboardHandler: KeyboardHandler!
Initialize it inside viewDidLoad()
:
keyboardHandler = KeyboardHandler(heightDidChange: { [weak self] (newKeyboardHeight: CGFloat, animationDuration: TimeInterval) in
if let strongSelf = self {
// animate UI
}
})
It is better to enable and disable it when your controller is appearing and disappering:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
keyboardHandler.isActive = true
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
keyboardHandler.isActive = false
}
On iPhone X exists new feature to quickly switch between apps. Quick change between two apps one of which has open keyboard leads to several notifications generated with wrong data. At the moment there is no way to detect these "bad" notifications. So, it is strongly advised to add additional check before you do your animations:
// inside KeyboardHandler's block
if let strongSelf = self {
if strongSelf.controlWithKeyboardIsEditing() || newKeyboardHeight == 0 {
// animate UI
}
}
// somewhere in your UIViewController
private func controlWithKeyboardIsEditing() -> Bool {
return someTextField.isEditing // or some other expression, that suits your needs
}
Shakuro.KeyboardHandler is released under the MIT license. See LICENSE for details.
Star this tool if you like it, it will help us grow and add new useful things. Feel free to reach out and hire our team to develop a mobile or web project for you.