Giter VIP home page Giter VIP logo

sbkeyboardevents's Introduction

SBKeyboardEvents

Version License Platform

SBKeyboardEvents makes it easy to repond to keyboard events without having to write the same boilerplate code in every view controller. No more subscribing to a bunch of Notifications in viewDidLoad!

Example

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

Installation

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

pod "SBKeyboardEvents"

Usage

Your ViewController (or other class, if you like), should implement the 'KeyboardEventListener' protocol. It should then register itself for keyboard events should with the SBKeyboardEvents singleton:

######(There are methods for unsubscribing a listener, but this is handled automatically when the class is deallocated, so in practice, these methods should very rarely be required. It is safe subscribe and forget. SBKeyboardEvents keeps a weak reference to listeners, so it will not retain them)

override func viewDidLoad() {
	super.viewDidLoad()
        
	SBKeyboardEvents.addListener(self)
}

For the simplest implementation, you only need to implement a single method, animateForKeyboardHeight(height:)

This method is called from within the keyboard appearance/disappearance animation block, so adjust constraints/frames, and the rest will be handled automatically. Note that the height here, is the keyboard height from the bottom of the screen.

extension GameSetupViewController: KeyboardEventListener {
    
    func animateForKeyboardHeight(_ height: CGFloat) {
         
        keyboardPaddingConstraint.constant = height;
        view.setNeedsLayout()
        view.layoutIfNeeded()
    }
}

This is the full Protocol declaration:

public protocol KeyboardEventListener : class {
    
    func keyboardWillAppear()
    func keyboardDidAppear()
    func keyboardWillHide()
    func keyboardDidHide()
    func keyboardWillChangeFrame(_ frame: CGRect)
    func keyboardDidChangeFrame(_ frame: CGRect)
    func animateForKeyboardFrame(_ frame: CGRect)
    func animateForKeyboardHeight(_ height: CGFloat)
}

Functions containing 'will' occur before the animation.

Functions containing 'did' occur after the animation.

Functions starting 'animate' are called from within the keyboard animation block

Author

Steve Barnegren, [email protected]

@SteveBarnegren

License

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

sbkeyboardevents's People

Contributors

stevebarnegren avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

carabina

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.