KeyboardAdjustable takes into account the two most common ways we adjust our view when the keyboard appears:
- When you have a ScrollView and you want to change its contentInset when the keyboard appears
- When you have a bottom constraint for a UIView and you want to change itβs value when the keyboard appears
With only one line of code you'll be able to handle these scenarios without having to implement the observers in your own UIViewController.
- Import KeyboardAdjustable on your UIViewController file
- Add the KeyboardAdjustable protocol to your UIViewController
- Setup a KeyboardAdjustingStrategy on your viewDidLoad
Note:
Make sure you're calling registerForKeyboardNotifications()
on your viewDidLoad and unregisterForKeyboardNotification()
on your viewDidDisappear.
class SingleConstraintViewController: UIViewController, KeyboardAdjustable {
var keyboardAdjustingStrategy: KeyboardAdjustingStrategy?
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
// Register for keyboard notifications
registerForKeyboardNotifications()
// Set your desired strategy to adjust the view when the keyboard appears
self.keyboardAdjustingStrategy = .singleConstraint(
constraint: bottomConstraint,
originalConstant: bottomConstraint.constant,
spaceAboveKeyboard: 20)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
// Unregister for keyboard notifications
unregisterForKeyboardNotification()
}
}
This Pod also has an example project
To run the example project, clone the repo, and run pod install
from the Example directory first.
With a ScrollView:
With a Bottom Constraint
Swift 4.2
KeyboardAdjustable is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'KeyboardAdjustable'
Cesar Bess, [email protected]
KeyboardAdjustable is available under the MIT license. See the LICENSE file for more info.