Giter VIP home page Giter VIP logo

ios-keyboardsupport's Introduction

KeyboardSupport

CI Status Version License Platform codecov codebeat badge

Purpose

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 or UITextView.
  • Easily implement navigation between text inputs by supplying your own input accessory view.
  • Allow keyboard "Return" key to navigate between UITextFields.
  • Provide a UIToolbar subclass so you can create your own input accessory views faster.

Key Concepts

  • 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 and KeyboardScrollable 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.

Usage

KeyboardDismissable

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()
        setupKeyboardDismissal()
    }
}

KeyboardScrollable

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()
    }
}

KeyboardToolbar

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 UIBarButtonItems.

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.

KeyboardNavigator - when using a KeyboardToolbar

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
    }
}

KeyboardNavigator - when using the keyboard's "Return" key

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)
    }
}

KeyboardAutoNavigator - when using a KeyboardToolbar

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 keyboardNavigatorDidTapBack(_ navigator: KeyboardAutoNavigator) {
// Your code here
}

func keyboardNavigatorDidTapNext(_ navigator: KeyboardAutoNavigator) {
// Your code here
}

func keyboardNavigatorDidTapDone(_ navigator: KeyboardAutoNavigator) {
// Your code here
}
}

Example

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

Requirements

  • iOS 9.0+
  • Swift 4.1

Installation

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

pod 'KeyboardSupport'

Author

Bottle Rocket Studios

License

KeyboardSupport is available under the Apache 2.0 license. See the LICENSE.txt file for more info.

Contributing

See the CONTRIBUTING document. Thank you, contributors!

ios-keyboardsupport's People

Contributors

br-johndavis avatar br-tyler-milner avatar br-earl-gaspard avatar wmcginty avatar ngoleo avatar tylermilner avatar earlgaspard avatar br-nchiu avatar achappell avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.