Skip to content
An elegant Keyboard library for iOS. simple, lightweight and standalone no sub-dependencies required
Swift Ruby Objective-C
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
CommonKeyboard.xcodeproj Update version to 1.0.3 Sep 10, 2019
Examples Completed project with examples Sep 9, 2019
Sources Update version to 1.0.4 Sep 12, 2019
Tests
.gitignore Initial commit Sep 6, 2019
CommonKeyboard.podspec Update version to 1.0.4 Sep 12, 2019
LICENSE.md
README.md Correct readme.md Sep 12, 2019

README.md

CommonKeyboard

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

Swift Swift

CommonKeyboard CommonKeyboardObserver

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

Contact

If you have any question or issue please create an issue!

License

CommonKeyboard is released under the MIT License.

You can’t perform that action at this time.