KeyboardAdjustable
KeyboardAdjustable takes into account the three 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
- When you have multiple constraints in your views and you want to change it's values when the keyboard appears
With only one line of code you'll be able to handle these scenarios without having to implement the keyboard observers in your own UIViewController.
How to use:
- 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 unregisterForKeyboardNotifications()
on your viewDidDisappear.
Example
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
unregisterForKeyboardNotifications()
}
}
This Pod also has an example project
To run the example project, clone the repo, and run pod install
from the Example directory first.
Demo
With a ScrollView:
With a Single Constraint:
With Multiple Constraints:
Requirements
Swift 4.2
Installation
KeyboardAdjustable is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'KeyboardAdjustable'
If you're having issues with the command above, try running pod repo update
then try again
Author
Cesar Bess, [email protected]
License
KeyboardAdjustable is available under the MIT license. See the LICENSE file for more info.