Code Monkey home page Code Monkey logo

keyboardadjustable's Introduction

KeyboardAdjustable

CI Status Version License Platform

KeyboardAdjustable takes into account the two most common ways we adjust our view when the keyboard appears:

  • When you have a ScrollView and you want to change its contentInset when the keyboard appears
  • When you have a bottom constraint for a UIView and you want to change it’s value when the keyboard appears

With only one line of code you'll be able to handle these scenarios without having to implement the observers in your own UIViewController.

How to use:

  1. Import KeyboardAdjustable on your UIViewController file
  2. Add the KeyboardAdjustable protocol to your UIViewController
  3. Setup a KeyboardAdjustingStrategy on your viewDidLoad

Note: Make sure you're calling registerForKeyboardNotifications() on your viewDidLoad and unregisterForKeyboardNotification() on your viewDidDisappear.

Example

class SingleConstraintViewController: UIViewController, KeyboardAdjustable {

    var keyboardAdjustingStrategy: KeyboardAdjustingStrategy?

    @IBOutlet weak var bottomConstraint: NSLayoutConstraint!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Register for keyboard notifications
        registerForKeyboardNotifications()
        
        // Set your desired strategy to adjust the view when the keyboard appears
        self.keyboardAdjustingStrategy = .singleConstraint(
                                          constraint: bottomConstraint, 
                                          originalConstant: bottomConstraint.constant,
                                          spaceAboveKeyboard: 20)
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        // Unregister for keyboard notifications
        unregisterForKeyboardNotification()
    }
}

This Pod also has an example project

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

Demo

With a ScrollView:

With a Bottom Constraint

Requirements

Swift 4.2

Installation

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

pod 'KeyboardAdjustable'

Author

Cesar Bess, [email protected]

License

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

keyboardadjustable's People

Contributors

cesarbess avatar

Stargazers

 avatar

Watchers

 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.