SimpleKeyboard 1.0.2

SimpleKeyboard 1.0.2

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jul 2018
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Cristian Sava.



  • By
  • Cristian Sava

SimpleKeyboard

Swift Version License Platform

SimpleKeyboard addresses a very common problem on the iOS platform: when the keyboard is shown, it can happen that the input field under it is not visible anymore. Since there is no simple or out-of-the-box general solution for this, SimpleKeyboard attempts to provide exactly that.

What makes SimpleKeyboard simple is the fact it works in every View Controller without requiring UIScrollView, AutoLayout, bottom-constraint or any other stuff that is present in different other solutions.

Example

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

Using UIScrollView

ScrollView

Login screen with bottom constraint

Login

TableView with cell containing UITextField

TableView

Usage

SimpleKeyboard is designed for flexibility, that's why there are 3 ways you can use it:

  • Automatic: takes away all the delegate/keyboard code from your ViewController. You just specify or configure the IBOutlets and you're done.
  • Manual: you have full control over all UITextField/UITextView in the ViewController. The keyboard still works nicely, but you have to notify SimpleKeyboard of the current input control.
  • Combined: SimpleKeyboard will manage some of the input controls for you, while the rest are left in your care.

Creation

First of all, import SimpleKeyboard into your View Controller and declare a new variable

var simpleKeyboard: SimpleKeyboard!

Now create the keyboard object in onViewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()
        
    simpleKeyboard = SimpleKeyboard.createKeyboard(forControls: [firstTextField, secondTextField,
                                                                 firstTextView, thirdTextField],
                                                   fromViewController: self)
}

If there's a need for a keyboard toolbar for a specific control (on a UITextView or a numeric keyboard), use the add function to let SimpleKeyboard manage it for you

simpleKeyboard.add(control: specificTextView, withDoneButtonKeyboard: true)
// Use selector when wanting to process pressing Done button
simpleKeyboard.add(control: lastVisibleTextField, withDoneButtonKeyboard: true, 
                   doneTarget: self, doneSelector: #selector(donePressed(_:)))

If you don't want SimpleKeyboard to manage anything, only to correctly handle appearing and disappearing of the keyboard, then simply instantiate without any control

simpleKeyboard = SimpleKeyboard(fromViewController: self)

Enabling/Disabling

Override viewDidAppear and viewDidDisappear in order to start and stop using SimpleKeyboard

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
        
    simpleKeyboard.enable()
}
    
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
        
    simpleKeyboard.disable()
}

But wait, I want the keyboard to disappear when I press Return key

You won't need delegates for that. SimpleKeyboard implements this in an elegant way using callbacks for catching different events. Just set up the callback like this

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
        
    simpleKeyboard.enable()
            
    simpleKeyboard.textFieldShouldReturn = { textField in
        textField.resignFirstResponder()
                
        return true
    }
}

Keep in mind this will work only for controls managed by SimpleKeyboard. All others are to be handled inside the View Controller using delegates.

Inception

What about the case when the View Controller manages different views and a UITableView, which also has different input controls in every cell? There are multiple source files with different outlets, what goes where?

It's easy:

  • Create keyboard (with or without specific controls) and enable it
  • Implement delegate functions for begining and ending using the keyboard
  • Inside these delegates, notify SimpleKeyboard of the current view which receives
simpleKeyboard.setActive(view: view)

and loses

simpleKeyboard.clearActiveView()

the focus.

For more details, check NoControlsViewController.swift from the example App.

Things to consider

  • Always disable the keyboard after enabling it. Failure to do so will most likely result in a crash.
  • Do not set the delegate for a control which is being handeled by SimpleKeyboard. Depending on code flow, it could render the benefits of SimpleKeyboard useless. If you need to handle a specific UITextField/UITextView yourself, use the setActive and clearActiveView functions accordingly.

Requirements

  • Swift 4.1
  • iOS 10.0 +
  • XCode 9.4+

Installation

CocoaPods

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

pod 'SimpleKeyboard'

or

pod 'SimpleKeyboard', '~> 1.0.2'

Manually

Simply copy SimpleKeyboard.swift to your project and you're ready to go.

Author

Cristian Sava, [email protected]

License

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