This library provides conveniences for dealing with common keyboard tasks. There are a few main goals:
- Make it easy to auto-dismiss the keyboard via tap on screen.
- Auto-scrolling to the active
UITextField
orUITextView
. - Easily implement navigation between text inputs by supplying your own input accessory view.
- Allow keyboard "Return" key to navigate between
UITextField
s. - Provide a
UIToolbar
subclass so you can create your own input accessory views faster.
- KeyboardDismissable - A protocol that enables automatic keyboard dismissal via tapping the screen when the keyboard is displayed.
- KeyboardScrollable - A protocol that enables scrolling views to the first responder when a keyboard is shown. Must be used with a
UIScrollView
or one of its subclasses. - KeyboardRespondable - Inherits from both
KeyboardDismissable
andKeyboardScrollable
for convenience. - KeyboardToolbar - A subclass of
UIToolbar
with customization options to quickly create your own input accessory views. - KeyboardAccessory - Have your custom view conform to this protocol to get callbacks for "back", "next", and "done" for moving between text inputs.
- KeyboardNavigator - Handles navigation between text inputs by providing your
KeyboardToolbar
or using the keyboard's return key.
Conform to this protocol to enable keyboard dismissal via tapping the screen when the keyboard is displayed.
class ViewController: UIViewController, KeyboardDismissable {
override func viewDidLoad() {
super.viewDidLoad()
setupKeyboardDismissalView()
}
}
Conform to this protocol to enable scrolling to the first responder when the keyboard is shown. Must be used with a UIScrollView
or one of its subclasses.
class ViewController: UIViewController, KeyboardScrollable {
@IBOutlet private var scrollView: UIScrollView!
var keyboardScrollableScrollView: UIScrollView? {
return scrollView
}
var keyboardWillShowObserver: NSObjectProtocol?
var keyboardWillHideObserver: NSObjectProtocol?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setupKeyboardObservers()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
removeKeyboardObservers()
}
}
Create your own input accessory view for navigation between text inputs. Use the convenience methods to create back/next/done buttons or supply your own UIBarButtonItem
s.
let keyboardToolbar = KeyboardToolbar()
keyboardToolbar.addButton(type: .back, title: "Back")
keyboardToolbar.addButton(type: .next, title: "Next")
keyboardToolbar.addFlexibleSpace()
keyboardToolbar.addSystemDoneButton()
Check out KeyboardToolbar
for other button adding options.
Create a KeyboardToolbar
, configuring it with back/next/done buttons as appropriate. Then, create a KeyboardNavigator
, passing in your text inputs and toolbar. The order of the text inputs determines the navigation order for traversing from one to the next. Optionally, implement KeyboardNavigatorDelegate
to receive call backs when tapping "Back", "Next", and "Done" in your KeyboardToolbar
.
class ViewController: UIViewController {
@IBOutlet private var textInput1: UITextField!
@IBOutlet private var textInput2: UITextView!
private var keyboardNavigator: KeyboardNavigator?
override func viewDidLoad() {
super.viewDidLoad()
let keyboardToolbar = KeyboardToolbar()
keyboardNavigator = KeyboardNavigator(textInputs: [textInput1, textInput2], keyboardToolbar: keyboardToolbar)
keyboardNavigator?.delegate = self
}
}
extension ViewController: KeyboardNavigatorDelegate {
func keyboardNavigatorDidTapBack(_ navigator: KeyboardNavigator) {
// Your code here
}
func keyboardNavigatorDidTapNext(_ navigator: KeyboardNavigator) {
// Your code here
}
func keyboardNavigatorDidTapDone(_ navigator: KeyboardNavigator) {
// Your code here
}
}
Create a KeyboardNavigator
, passing in your text inputs and setting the returnKeyNavigationEnabled
parameter to true
. The order of the text fields determines the navigation order for traversing from one text input to the next. It's important to note that the use of the KeyboardToolbar
and the keyboard's "Return" keys are not mutually exclusive. You can have a KeyboardNavigator
use both a KeyboardToolbar
and the keyboard's "Return" keys.
class ViewController: UIViewController {
@IBOutlet private var textInput1: UITextField!
@IBOutlet private var textInput2: UITextField!
private var keyboardNavigator: KeyboardNavigator?
override func viewDidLoad() {
super.viewDidLoad()
keyboardNavigator = KeyboardNavigator(textInputs: [textInput1, textInput2], returnKeyNavigationEnabled: true)
}
}
Create a KeyboardToolbar
, configuring it with back/next/done buttons as appropriate. Then, create a KeyboardAutoNavigator
, passing in your toolbar. The position of the text inputs determines the navigation order for traversing from one to the next. Optionally, implement KeyboardAutoNavigatorDelegate
to receive call backs when tapping "Back", "Next", and "Done" in your KeyboardToolbar
.
class ViewController: UIViewController {
@IBOutlet private var textInput1: UITextField!
@IBOutlet private var textInput2: UITextView!
private var keyboardNavigator: KeyboardAutoNavigator?
override func viewDidLoad() {
super.viewDidLoad()
let keyboardToolbar = KeyboardToolbar(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 44.0))
keyboardNavigator = KeyboardAutoNavigator(navigationContainer: scrollView, defaultToolbar: keyboardToolbar, returnKeyNavigationEnabled: true)
keyboardNavigator?.delegate = self
}
}
extension ViewController: KeyboardAutoNavigatorDelegate {
func keyboardAutoNavigatorDidTapBack(_ navigator: KeyboardAutoNavigator) {
// Your code here
}
func keyboardAutoNavigatorDidTapNext(_ navigator: KeyboardAutoNavigator) {
// Your code here
}
func keyboardAutoNavigatorDidTapDone(_ navigator: KeyboardAutoNavigator) {
// Your code here
}
}
To run the example project, clone the repo, and run pod install
from the Example directory first.
- iOS 9.0+
- Swift 5.0
dependencies: [
.package(url: "https://github.com/BottleRocketStudios/iOS-KeyboardSupport.git", from: "2.1.1")
]
KeyboardSupport is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'KeyboardSupport'
Add the following to your Cartfile:
github "BottleRocketStudios/iOS-KeyboardSupport"
Run carthage update
and follow the steps as described in Carthage's README.
KeyboardSupport is available under the Apache 2.0 license. See the LICENSE.txt file for more info.
See the CONTRIBUTING document. Thank you, contributors!