- iOS 8.0+
- Xcode 9.0+
- Swift 4.0+
To integrate UIKeyboard
into your Xcode project using CocoaPods, create Podfile
. Run the following command in root folder of your project:
$ pod init
In the Podfile
that appears, specify:
platform :ios, ‘8.0’
target '<Your Target Name>' do
use_frameworks!
pod 'UIKeyboard'
end
Then, run the following command:
$ pod install
- By default
UIViewController
haveUIKeyboard
property withkeyboard
name which notify about keyboard appearance changes
class UIKeyboardViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Default UIViewController keyboard property
self.keyboard
}
}
- You can use
UIKeyboard
observers with closure or(and) delegate. Choose a method that is more convenient for you.
- with
UIKeyboardDelegate
:
class UIKeyboardViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Add delegate to default UIViewController keyboard property
self.keyboard.delegate = self
}
}
extension UIKeyboardViewController: UIKeyboardDelegate {
func keyboardWillShow(with info: UIKeyboard.Info) {
// Posted immediately prior to the display of the keyboard
}
func keyboardDidShow(with info: UIKeyboard.Info) {
// Posted immediately after the display of the keyboard.
}
func keyboardWillHide(with info: UIKeyboard.Info) {
// Posted immediately prior to the dismissal of the keyboard.
}
func keyboardDidHide(with info: UIKeyboard.Info) {
// Posted immediately after the dismissal of the keyboard.
}
}
- with closures
class UIKeyboardViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.keyboard.willShow { (info) in
// Posted immediately prior to the display of the keyboard
}
self.keyboard.didShow { (info) in
// Posted immediately after the display of the keyboard.
}
self.keyboard.willHide { (info) in
// Posted immediately prior to the dismissal of the keyboard.
}
self.keyboard.didHide { (info) in
// Posted immediately after the dismissal of the keyboard.
}
}
}
UIKeyboard.Info
have next properties:
/// Rect that identifies the ending frame rectangle of the keyboard in screen coordinates. The frame rectangle reflects the current orientation of the device.
public let frame: CGRect
/// Time interval that identifies the duration of the animation in seconds.
public let animationDuration: TimeInterval
/// Animation curve constant that defines how the keyboard will be animated onto or off the screen.
public let animationCurve: UIView.AnimationCurve
/// Animation options constant that defines how the keyboard will be animated onto or off the screen.
public var animationOptions: UIView.AnimationOptions
- Simple example of
info
usege for changescrollView
content inset:
self.keyboard.willShow { (info) in
UIView.animate(withDuration: info.animationDuration, delay: 0, options: info.animationOptions, animations: {
self.scrollView.contentInset.bottom = info.frame.height
}, completion: nil)
}
Released under the MIT license. See LICENSE for details.