This library provides conveniences for dealing with common keyboard tasks. There are a few main goals:
UITextField
or UITextView
.UITextField
s.UIToolbar
subclass so you can create your own input accessory views faster.UIScrollView
or one of its subclasses.KeyboardDismissable
and KeyboardScrollable
for convenience.UIToolbar
with customization options to quickly create your own input accessory views.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.
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!
link |
Stars: 15 |
Last commit: 1 year ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics