Skip to content

An iOS cocoapod that uses a constraint to move a view out of the way of the on-screen keyboard.

License

Notifications You must be signed in to change notification settings

TradeMe/KeyboardDodger

Repository files navigation

KeyboardDodger

KeyboardDodger is an iOS cocoapod that uses a constraint to move a view out of the way of the on-screen keyboard.

Installation

pod 'KeyboardDodger', '~> 1.0'

Usage

KeyboardDodger attaches to a view and a constraint at the bottom of the view, and manipulates the constraint to keep it out of the way of the on-screen keyboard.

An example implementation:

class ViewController: UIViewController {

    var bottomConstraint: NSLayoutConstraint?

    var keyboardDodger: KeyboardDodger?

    override func viewDidLoad() {
        super.viewDidLoad()

        if let bottomConstraint = bottomConstraint {
            keyboardDodger = KeyboardDodger(view: view, constraint: bottomConstraint, delegate: self)
        }
    }

}

extension ViewController: KeyboardDodgerDelegate {
    
    func keyboardDodger(_ keyboardDodger: KeyboardDodger, willUpdateConstraintWith transition: KeyboardDodgerTransition) {
        print("Keyboard dodger will update constraint")
    }
    
    func keyboardDodger(_ keyboardDodger: KeyboardDodger, didUpdateConstraintWith transition: KeyboardDodgerTransition) {
        print("Keyboard dodger did update constraint")
    }
    
    func keyboardDodger(_ keyboardDodger: KeyboardDodger, willResetConstraintWith transition: KeyboardDodgerTransition) {
        print("Keyboard dodger will reset constraint")
    }
    
    func keyboardDodger(_ keyboardDodger: KeyboardDodger, didResetConstraintWith transition: KeyboardDodgerTransition) {
        print("Keyboard dodger did reset constraint")
    }

}