For less complicated keyboard event handling.
Clone or download
morizotter Merge pull request #13 from emre-yilmaz/master
Update to Swift syntax
Latest commit 6cb13a8 Sep 29, 2017


For less complicated keyboard event handling.

Carthage compatible


  • Less complicated keyboard event handling.
  • Do not use Notification , but event .


Without KeyboardObserver.swift

let keyboardNotifications: [Notification.Name] = [

override func viewDidLoad() {

override func viewWillAppear(animated: Bool) {

    keyboardNotifications.forEach {
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardEventNotified:), name: $0, object: nil)

override func viewWillDisappear(animated: Bool) {

    keyboardNotifications.forEach {
        NotificationCenter.default.removeObserver(self, name: $0, object: nil)

func keyboardEventNotified(notification: Notification) {
    guard let userInfo = notification.userInfo else { return }
    let keyboardFrameEnd = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
    let curve = UIViewAnimationCurve(rawValue: (userInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).integerValue)!
    let options = UIViewAnimationOptions(rawValue: UInt(curve.rawValue << 16))
    let duration = TimeInterval((userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue)
    let distance = UIScreen.main.bounds.height - keyboardFrameEnd.origin.y
    let bottom = distance >= bottomLayoutGuide.length ? distance : bottomLayoutGuide.length

    UIView.animate(withDuration: event.duration, delay: 0.0, options: [event.options], animations:
         { () -> Void in
            self?.textView.contentInset.bottom = bottom
            self?.textView.scrollIndicatorInsets.bottom = bottom
        } , completion: nil)

With KeyboardObserver

let keyboard = KeyboardObserver()

override func viewDidLoad() {

    keyboard.observe { [weak self] (event) -> Void in
        guard let s = self else { return }
        switch event.type {
        case .willShow, .willHide, .willChangeFrame:
            let distance = UIScreen.main.bounds.height - event.keyboardFrameEnd.origin.y
            let bottom = distance >= s.bottomLayoutGuide.length ? distance : s.bottomLayoutGuide.length

            UIView.animateWithDuration(event.duration, delay: 0.0, options: [event.options], animations:
                { () -> Void in
                    self?.textView.contentInset.bottom = bottom
                    self?.textView.scrollIndicatorInsets.bottom = bottom
                } , completion: nil)

How to use

Create KeyboardObserver instance where you want, and the instance observes keyboard untill deinit.

Call observe(event: KeyboardEvent) to observe keyboard events. event is converted keyboard notification object.

public struct KeyboardEvent {
    public let type: KeyboardEventType
    public let keyboardFrameBegin: CGRect
    public let keyboardFrameEnd: CGRect
    public let curve: UIViewAnimationCurve
    public let duration: NSTimeInterval
    public var isLocal: Bool?

    public var options: UIViewAnimationOptions {
        return UIViewAnimationOptions(rawValue: UInt(curve.rawValue << 16))

event has properties above. You don't have to convert Notification 's userInfo to extract keyboard event values.

KeyboardEentType has types same as keyboard's notification name. Like this below:

public enum KeyboardEventType {
    case willShow
    case didShow
    case willHide
    case didHide
    case willChangeFrame
    case didChangeFrame

It has also public var notificationName: String which you can get original notification name.

Runtime Requirements

  • iOS 8.0 or later
  • Xcode 9.0 - Swift4

Installation and Setup

Information: To use KeyboardObserver with a project targeting lower than iOS 8.0, you must include the KeyboardObserver.swift source file directly in your project.

Installing with Carthage

Just add to your Cartfile:

github "morizotter/KeyboardObserver"

Installing with CocoaPods

CocoaPods is a centralised dependency manager that automates the process of adding libraries to your Cocoa application. You can install it with the following command:

$ gem update
$ gem install cocoapods
$ pods --version

To integrate KeyboardObserver into your Xcode project using CocoaPods, specify it in your Podfile and run pod install.

platform :ios, '8.0'
pod "KeyboardObserver", '~>1.0.0'

Manual Installation

To install KeyboardObserver without a dependency manager, please add KeyboardObserver.swift to your Xcode Project.


Please file issues or submit pull requests for anything you’d like to see! We're waiting! :)


KeyboardObserver.swift is released under the MIT license. Go read the LICENSE file for more information.