CommonKeyboard 1.0.8

CommonKeyboard 1.0.8

Maintained by Kaweerut Kanthawong, Kaweerut Kanthawong, Kaweerut Kanthawong.



  • By
  • Kaweerut Kanthawong

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 this single line of code below
    // supported UIScrollView including inherited classes (e.g., UITableView or UICollectionView)
    //
    // *** This doesn't work with UITableViewController because they've built-in handler ***
    //
    CommonKeyboard.shared.enabled = true
    
    // uncomment this line to print the debug logs
    //CommonKeyboard.shared.debug = true
      
    return true
  }
}

CommonKeyboard will automatically scroll to the input view when the cursor focused and tapping on on empty space to dismiss keyboard. This working with UIScrollView and all subclasses 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 self = self else { return }
            let bottom = info.isShowing
                ? (-info.visibleHeight) + self.view.safeAreaInsets.bottom
                : 0
            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 Tree 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
    }
}

Misc

 // dismiss keyboard
 CommonKeyboard.shared.dismiss()

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

Debugging

// enable debug mode to print out keyboard info
 CommonKeyboard.shared.debug = true


// ** Sample output **

/*
----- CommonKeyboard debug enabled -----
- isShowing:  true
- keyboardFrameBegin:  (0.0, 896.0, 414.0, 243.0)
- keyboardFrameEnd:  (0.0, 550.0, 414.0, 346.0)
- visibleHeight:  346.0
- isLocal:  true
- scrollContainer:  <UITableView: 0x103820e00; frame = (0 92; 414 700); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x28223a310>; layer = <CALayer: 0x282cf2960>; contentOffset: {0, 0}; contentSize: {414, 0}; adjustedContentInset: {0, 0, 0, 0}; dataSource: (null)>
------
*/

// ** Sample output when CommonKeyboard could not find `scrollContainer` **

/*
 ----- CommonKeyboard debug enabled -----
- isShowing:  true
- keyboardFrameBegin:  (0.0, 896.0, 414.0, 243.0)
- keyboardFrameEnd:  (0.0, 550.0, 414.0, 346.0)
- visibleHeight:  346.0
- isLocal:  true
- scrollContainer:    
   ***** 
     COULD NOT FIND `scrollContainer` 
     YOU BETTER TO IMPLEMENT `CommonKeyboardContainerProtocol` 
     IN `topViewController` (<KeyboardExample.FormViewController: 0x10570a150> 
     TO RETURN SPECIFIC `scrollContainer` 
   *****
------
*/

Requirements

  • iOS12 or later
  • Swift 5.0 or later

Contact

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

License

CommonKeyboard is released under the MIT License.