Skip to content

A Swift library that automatically resizes and adjusts views to scroll when a keyboard appears.

License

Notifications You must be signed in to change notification settings

lionheart/KeyboardAdjuster

Repository files navigation

CI Status Version License Platform Swift

KeyboardAdjuster provides a drop-in UILayoutGuide that helps you adjust your views to avoid the keyboard. That's pretty much all there is to it. It's battle-tested and easy to integrate into any project--Storyboards or code, doesn't matter.

KeyboardAdjuster started as a Swift port of LHSKeyboardAdjuster, which is recommended for projects written in Objective-C.

Requirements

  • Auto Layout
  • iOS 9.0-11.2+

Installation

KeyboardAdjuster is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "KeyboardAdjuster", "~> 3"

Usage

  1. In your view controller file, import KeyboardAdjuster.

    import KeyboardAdjuster
  2. Figure out which view you'd like to pin to the top of the keyboard--it's probably going to be a UIScrollView, UITableView, or UITextView. Then, wherever you're setting up your view constraints, use the keyboardLayoutGuide property to create a greaterThanOrEqualTo constraint to the bottom of the view you'd like to resize:

    class MyViewController: UIViewController {
        func viewDidLoad() {
            super.viewDidLoad()
    
            // ...
            // Your Auto Layout code here
            // ...
    
            tableView.bottomAnchor.constraint(lessThanOrEqualTo: view.bottomAnchor).isActive = true
            tableView.bottomAnchor.constraint(greaterThanOrEqualTo: keyboardLayoutGuide.topAnchor).isActive = true
        }
    }
    NOTE: If you're using iOS 11 and your view is using the safeAreaLayoutGuide to set constraints, click here to view an alternate approach.
    func viewDidLoad() {
        super.viewDidLoad()
    
        tableView.bottomAnchor.constraint(lessThanOrEqualTo: keyboardLayoutGuide.topAnchor).isActive = true
    }
  3. And you're done! Whenever a keyboard appears, your view will be automatically resized.

Optional Features

KeyboardAdjuster also allows you to provide callbacks when the keyboard state changes or specify whether to animate the transition (animated by default). If you'd like to take advantage of these, just make your UIViewController conform to KeyboardAdjusterOptions, like so:

class MyViewController: UIViewController, KeyboardAdjusterOptions {
    var animateKeyboardTransition = true

    func keyboardWillHideHandler() {
        print("Hiding keyboard...")
    }

    func keyboardWillShowHandler() {
        print("Showing keyboard...")
    }
}

How It Works

KeyboardAdjuster registers NSNotificationCenter callbacks for keyboard appearance and disappearance. When a keyboard appears, it pulls out the keyboard size from the notification, along with the duration of the keyboard animation, and applies that to the keyboardLayoutGuide property.

Support KeyboardAdjuster

Supporting KeyboardAdjuster, keeping it up to date with the latest iOS versions, etc., takes a lot of time! So, if you're a developer who's gotten some utility out of this library, please support it by starring the repo. This increases its visibility in GitHub search and encourages others to contribute. 🙏🏻🍻

Author

Dan Loewenherz

License

KeyboardAdjuster is available under the Apache 2.0 LICENSE. See the LICENSE file for more info.