Latest 0.3
License MIT
Platforms ios 9.0
Frameworks UIKit


SBKeyboardEvents makes it easy to repond to keyboard events without having to write the same boilerplate code in every view controller. No more subscribing to a bunch of Notifications in viewDidLoad!


To run the example project, clone the repo, and run pod install from the Example directory first.


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

pod "SBKeyboardEvents"


Your ViewController (or other class, if you like), should implement the ‘KeyboardEventListener’ protocol. It should then register itself for keyboard events should with the SBKeyboardEvents singleton:

(There are methods for unsubscribing a listener, but this is handled automatically when the class is deallocated, so in practice, these methods should very rarely be required. It is safe subscribe and forget. SBKeyboardEvents keeps a weak reference to listeners, so it will not retain them)
override func viewDidLoad() {


For the simplest implementation, you only need to implement a single method, animateForKeyboardHeight(height:)

This method is called from within the keyboard appearance/disappearance animation block, so adjust constraints/frames, and the rest will be handled automatically. Note that the height here, is the keyboard height from the bottom of the screen.

extension GameSetupViewController: KeyboardEventListener {

    func animateForKeyboardHeight(_ height: CGFloat) {

        keyboardPaddingConstraint.constant = height;

This is the full Protocol declaration:

public protocol KeyboardEventListener : class {

    func keyboardWillAppear()
    func keyboardDidAppear()
    func keyboardWillHide()
    func keyboardDidHide()
    func keyboardWillChangeFrame(_ frame: CGRect)
    func keyboardDidChangeFrame(_ frame: CGRect)
    func animateForKeyboardFrame(_ frame: CGRect)
    func animateForKeyboardHeight(_ height: CGFloat)

Functions containing ‘will’ occur before the animation.

Functions containing ‘did’ occur after the animation.

Functions starting ‘animate’ are called from within the keyboard animation block


Steve Barnegren, [email protected]



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

Latest podspec

    "name": "SBKeyboardEvents",
    "version": "0.3",
    "summary": "Easily respond to keyboard events",
    "description": "Write less boilerplate code and easily respond to keyboard events",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": {
        "Steve Barnegren": "[email protected]"
    "source": {
        "git": "",
        "tag": "0.3"
    "social_media_url": "",
    "platforms": {
        "ios": "9.0"
    "source_files": "SBKeyboardEvents/SBKeyboardEvents/**/*.swift",
    "swift_versions": "5.0",
    "frameworks": "UIKit",
    "swift_version": "5.0"

Pin It on Pinterest

Share This