simsaens / KeyboardAdjuster

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

KeyboardAdjuster

CI Status Version Platform Swift

KeyboardAdjuster will adjust the bottom position of any given UIView when a keyboard appears on screen. All you have to do is provide an NSLayoutConstraint that pins the bottom of your view to the bottom of the screen. KeyboardAdjuster will automatically adjust that constraint and pin it to the top of the keyboard when it appears.

If you're currently choosing between KeyboardAdjuster and another alternative, please read about our philosophy

Note: KeyboardAdjuster requires layout anchors in your build target, so it will only work with iOS 9 or above. If you'd like to add support for earlier iOS versions, please submit a pull request.

KeyboardAdjuster is a Swift port of LHSKeyboardAdjuster, which targets projects written in Objective-C.

Installation

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

pod "KeyboardAdjuster"

Usage

  1. In your view controller file, import KeyboardAdjuster.

    import KeyboardAdjuster
  2. Make sure your UIViewController conforms to KeyboardAdjuster, and define two properties. Set keyboardAdjusterAnimated to true or false, depending on whether you want the constraint adjustment to be animated.

    class MyViewController: UIViewController, KeyboardAdjuster {
        var keyboardAdjusterConstraint: NSLayoutConstraint?
        var keyboardAdjusterAnimated: Bool? = false
    
        // ...
    }
  3. Figure out which view you'd like to pin to the top of the keyboard. A UIScrollView, UITableView, or UITextView are likely candidates. Then, wherever you're setting up your view constraints, set keyboardAdjusterConstraint to the constraint pinning the bottom of this view to the bottom of the screen:

    class MyViewController: UIViewController, KeyboardAdjuster {
        func viewDidLoad() {
            super.viewDidLoad()
    
            keyboardAdjusterConstraint = view.bottomAnchor.constraintEqualToAnchor(scrollView.bottomAnchor)
        }
    }

    KeyboardAdjuster will automatically activate keyboardAdjusterConstraint for you.

  4. All you need to do now is activate and deactivate the automatic adjustments in your viewWillAppear(animated:) and viewWillDisappear(animated:) methods.

    class MyViewController: UIViewController, KeyboardAdjuster {
        override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated)
            activateKeyboardAdjuster()
        }
    
        override func viewWillDisappear(animated: Bool) {
            super.viewWillDisappear(animated)
            deactivateKeyboardAdjuster()
        }
    }
  5. And you're done! Whenever a keyboard appears, your views will be automatically resized.

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 view constraint adjustment.

Author

Dan Loewenherz

License

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

About

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

License:Apache License 2.0


Languages

Language:Swift 81.9%Language:Shell 7.5%Language:Objective-C 6.2%Language:Ruby 4.3%