Latest 4.0.0
Homepage https://github.com/lionheart/KeyboardAdjuster
License Apache 2.0
Platforms ios 9.3, requires ARC
Authors

KeyboardAdjuster
KeyboardAdjuster

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

  • [x] Auto Layout
  • [x] 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.

    “`swift
    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.


Latest podspec

{
    "name": "KeyboardAdjuster",
    "version": "4.0.0",
    "summary": "Automatically resizes and adjust views to scroll when a keyboard appears.",
    "homepage": "https://github.com/lionheart/KeyboardAdjuster",
    "license": {
        "type": "Apache 2.0",
        "file": "LICENSE"
    },
    "authors": {
        "Dan Loewenherz": "[email protected]"
    },
    "source": {
        "git": "https://github.com/lionheart/KeyboardAdjuster.git",
        "tag": "4.0.0"
    },
    "social_media_url": "https://twitter.com/dwlz",
    "documentation_url": "https://code.lionheart.software/KeyboardAdjuster/",
    "platforms": {
        "ios": "9.3"
    },
    "requires_arc": true,
    "pod_target_xcconfig": {
        "SWIFT_VERSION": "4.2"
    },
    "swift_version": "4.2",
    "source_files": "Pod/Classes/**/*"
}

Pin It on Pinterest

Share This