A custom UITableView
subclass that enables keyboard-based navigation and selection.
UIKeyCommandTableView allows you to use hardware keyboard shortcuts to navigate and interact with table views. It supports customizable key commands and delegate callbacks for handling selection and activation events.
- Keyboard navigation with arrow keys.
- Selection and activation using space bar or return key.
- Customizable key command options.
- Delegate support for responding to key command events.
- Built-in tests ensuring reliability.
UIKeyCommandTableView is available via Swift Package Manager. Add the following dependency to your Package.swift:
dependencies: [
.package(url: "https://github.com/ipedro/UIKeyCommandTableView.git", from: "1.0.0")
]
Then, add "UIKeyCommandTableView"
as a dependency for your target.
Import the package into your view controller:
import UIKeyCommandTableView
Use UIKeyCommandTableView as you would any UITableView and implement the UITableViewKeyCommandsDelegate
to respond to first responder events and key command actions.
Instantiate and add a UIKeyCommandTableView to your view controller:
class MyViewController: UIViewController, UITableViewKeyCommandsDelegate {
let tableView = UIKeyCommandTableView()
override func viewDidLoad() {
super.viewDidLoad()
// Setup table view frame, dataSource, delegate, etc.
tableView.frame = view.bounds
tableView.keyCommandsDelegate = self
view.addSubview(tableView)
}
// UITableViewKeyCommandsDelegate methods
func tableViewDidBecomeFirstResponder(_ tableView: UIKeyCommandTableView) {
// handle event
}
func tableViewDidResignFirstResponder(_ tableView: UIKeyCommandTableView) {
// handle event
}
func tableViewKeyCommandSelectionBelowBounds(_ tableView: UIKeyCommandTableView) -> UIKeyCommandTableView.OutOfBoundsBehavior {
return .wrapAround
}
func tableViewKeyCommandSelectionAboveBounds(_ tableView: UIKeyCommandTableView) -> UIKeyCommandTableView.OutOfBoundsBehavior {
return .wrapAround
}
}
Customize key command options to fit your needs:
// In your view controller or table view setup:
tableView.selectPreviousKeyCommandOptions = [.arrowUp, .command(.arrowUp)]
tableView.selectNextKeyCommandOptions = [.arrowDown, .command(.arrowDown)]
tableView.activateSelectionKeyCommandOptions = [.spaceBar, .return]
- iOS 11 or later
This project is licensed under the MIT License. See the LICENSE file for details.