Skip to content
This repository was archived by the owner on Mar 10, 2022. It is now read-only.

No more checking for keyboard notifications and parsing keyboard apperance info manually!

License

Notifications You must be signed in to change notification settings

ml-opensource/KeyboardHelper

Repository files navigation

KeyboardHelper

No more checking for keyboard notifications and parsing keyboard apperance info manually!

A small (but cool) tool for handling UIKeyboard appearing and disappearing in your view controllers.

Travis Codecov Documentation CocoaPods Carthage Compatible Swift Package Manager Plaform GitHub license Readme Score

πŸ“¦ Installation

Carthage

github "nodes-ios/KeyboardHelper"

CocoaPods

pod 'KeyboardHelper', '~> 0.9'

Swit Package Manager

To use KeyboardHelper as a Swift Package Manager package just add the following to your Package.swift file.

import PackageDescription

let package = Package(
    name: "YourPackage",
    dependencies: [
        .Package(url: "https://github.com/nodes-ios/KeyboardHelper.git", majorVersion: 0)
    ]
)

NOTE: This doesn't currently work as SPM doesn't support iOS, but once it will we will already be supporting it! :)

πŸ”§ Setup

Implement KeyboardNotificationDelegate in your UIViewController.

class ViewController: UIViewController, KeyboardNotificationDelegate

Add a KeyboardHelper private variable and initialize it, setting the delegate.

private var keyboardHelper : KeyboardHelper?
...
self.keyboardHelper = KeyboardHelper(delegate: self)

Implement the two methods in the KeyboardNotificationDelegate:

public func keyboardWillAppear(info: KeyboardHelper.KeyboardAppearanceInfo)
public func keyboardWillDisappear(info: KeyboardHelper.KeyboardAppearanceInfo)

Both methods take as argument a KeyboardAppearanceInfo object, which is basically a wrapper over the userInfo dictionary of the UIKeyboardWillShowNotification and UIKeyboardWillHideNotification notifications.

One example of implementation for the two delegate methods is:

func keyboardWillAppear(info: KeyboardAppearanceInfo) {
        UIView.animateWithDuration(NSTimeInterval(info.animationDuration),
            delay: 0,
            options: info.animationOptions,
            animations: {
                let insets = UIEdgeInsetsMake(0, 0, info.endFrame.size.height, 0)
                self.scrollView.contentInset = insets
                self.scrollView.scrollIndicatorInsets = insets
            },
            completion:nil)
    }
    
    func keyboardWillDisappear(info: KeyboardAppearanceInfo) {
        UIView.animateWithDuration(NSTimeInterval(info.animationDuration),
            delay: 0,
            options: info.animationOptions,
            animations: {
                let insets = UIEdgeInsetsZero
                self.scrollView.contentInset = insets
                self.scrollView.scrollIndicatorInsets = insets
            },
            completion:nil)
    }

The KeyboardAppearanceInfo object has the following properties:

  • beginFrame: a CGRect corresponding to the value for UIKeyboardFrameBeginUserInfoKey
  • endFrame : a CGRect corresponding to the value for UIKeyboardFrameEndUserInfoKey
  • belongsToCurrentApp : a Bool corresponding to the value for UIKeyboardIsLocalUserInfoKey
  • animationDuration : a Double corresponding to the value for UIKeyboardAnimationDurationUserInfoKey
  • animationCurve : a UIViewAnimationCurve corresponding to the value for UIKeyboardAnimationCurveUserInfoKey
  • animationOptions : a UIViewAnimationOptions from the value of UIKeyboardAnimationCurveUserInfoKey

KeyboardAppearanceInfo also has the convenience method animateAlong:completion:, which can be used like this:

func keyboardWillAppear(info: KeyboardAppearanceInfo) {
	info.animateAlong({ () -> Void in
            let insets = UIEdgeInsetsMake(0, 0, info.endFrame.size.height, 0)
            self.scrollView.contentInset = insets
            self.scrollView.scrollIndicatorInsets = insets
        })  { finished in }

to get the same effect as the initial keyboardWillAppear: implementation example above.

πŸ‘₯ Credits

Made with ❀️ at Nodes.

πŸ“„ License

KeyboardHelper is available under the MIT license. See the LICENSE file for more info.