/ Keybaord

An elegant Keyboard library for iOS

An elegant Keyboard library for iOS

CommonKeyboard

An elegant Keyboard library for iOS. simple, lightweight and standalone no sub-dependencies required.

CommonKeyboardv

CommonKeyboard

Installation

CocoaPods

Add the following to your Podfile

pod 'CommonKeyboard'

Carthage

Add the following to your Cartfile

github "kaweerutk/CommonKeyboard"

Usage

In AppDelegate.swift, just import CommonKeyboard framework and enable CommonKeyboard.

import CommonKeyboard

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        // Just enable a single line of code
        CommonKeyboard.shared.enabled = true

        return true
    }
}

CommonKeyboard will automatically scroll to the input view when the cursor focused and tapping on a space to dismiss keyboard. This working with UIScrollView and all inheritance classes including UITableView and UICollectionView
(Note: This does not support UITableViewController because it will handle by itself)

Adjust an offset between keyboard and input view by set keyboardOffset the default value is 10, Or ignore common keyboard by giving ignoredCommonKeyboard a true value.

 textField.keyboardOffset = 20
 textField.ignoredCommonKeyboard = true

 textView.keyboardOffset = 2
 textView.ignoredCommonKeyboard = false

CommonKeyboardObserver

You can subscribe CommonKeyboardObserver to get keyboard notification info.

import CommonKeyboard

class ExampleChatViewController: UIViewController {

    @IBOutlet var tableView: UITableView!
    @IBOutlet var bottomConstraint: NSLayoutConstraint!
    let keyboardObserver = CommonKeyboardObserver()

    override func viewDidLoad() {
        super.viewDidLoad()
        // drag down to dismiss keyboard
        tableView.keyboardDismissMode = .interactive

        keyboardObserver.subscribe(events: [.willChangeFrame, .dragDown]) { [weak self] (info) in
            guard let weakSelf = self else { return }
            var bottom = 0
            if info.isShowing {
                bottom = -info.visibleHeight
                if #available(iOS 11, *) {
                    bottom += weakSelf.view.safeAreaInsets.bottom
                }
            }
            UIView.animate(info, animations: { [weak self] in
                self?.bottomConstraint.constant = bottom
                self?.view.layoutIfNeeded()
            })
        }
    }

}

All events

public enum CommonKeyboardObserverEvent {
    case willShow
    case didShow
    case willHide
    case didHide
    case willChangeFrame
    case didChangeFrame
    case dragDown // scroll.keyboardDismissMode = .interactive
}

Sometimes there are many UIScrollView containers in UI Stack View and the CommonKeyboard cannot find the right one you can implement CommonKeyboardContainerProtocol and return specific container

extension ExampleChatViewController: CommonKeyboardContainerProtocol {
    var scrollViewContainer: UIScrollView {
        return tableView
    }
}

Others

 // dismiss keyboard
 CommonKeyboard.shared.dismiss()

 // get current UIResponder
 let responder = CommonKeyboard.shared.currentResponder

Requirements

  • iOS9 or later
  • Swift 4.2 or later

GitHub