Coder Social home page Coder Social logo

ihkeyboardavoiding's Introduction

An elegant solution for keeping any UIView visible when the keyboard is being shown

alt demo

Description

IHKeyboardAvoiding will translate any UIView up when the keyboard is being shown, then return it when the keyboard is hidden.

Two views are registered with IHKeyboardAvoiding, the 'avoiding' UIView which is moved, and one or more 'target' UIViews which will trigger the avoiding. If a target view's frame will be intersected by the keyboard, then the avoiding view will move up just above the keyboard.

What sets IHKeyboardAvoiding apart from other keyboard avoiding solutions is that it doesn't require placing your content in a UIScrollView. No scroll view is used. And it isn't restricted to keeping UITextFields visible, instead any UIView can be a target which will be kept visible

If Autolayout is used then the constraints are animated, otherwise a CGAffine translation is done.

Supported Features:

  • iPhone keyboard
  • iPad docked keyboard
  • iPad undocked keyboard
  • iPad split keyboard
  • landscape & protrait
  • 3rd party keyboards
  • autolayout
  • traditional layout

How to install

Add this to your CocoaPods Podfile.

pod 'IHKeyboardAvoiding'

How to use:

To set the avoiding view

[IHKeyboardAvoiding setAvoidingView:(UIView *)avoidingView with:(UIView *)targetView];

To add another target

[IHKeyboardAvoiding addTarget:(UIView *)targetView];

Parameters
(UIView *)avoidingView The view to move above, usually the background view
(UIView *)targetView If a targetView's frame will be intersected by the keyboard, then the avoidingView will be moved so that the targetView is above the keyboard. Usually you'll set the avoidingView and targetView as the same view.

Optional methods
(BOOL)isKeyboardVisible A convenience method to check if the keyboard is visible
(void)setBuffer:(int)buffer The avoidingView will move if the keyboard is within [buffer] points of the targetView's frame. Default buffer is 0
(void)setPadding:(int)buffer The padding to put between the keyboard and target view. Default padding is 0

IHKeyboardAvoiding vs UIScrollView solutions (Fight, fight!):

UIScrollView pros:

  • They're quick n easy

IHKeyboardAvoiding pros:

  • You dont have scrollviews littered throughout your app
  • Having multiple scrollviews in your view heirarchy can cause problems
  • When the keyboard hides, scrollviews dont always scroll back to their original position
  • Scrollviews only scroll enough to keep the focused textfield visible
  • IHKeyboardAvoiding provides control over which UIViews are visible when the keyboard appears

Similar keyboard avoiding solutions:

https://github.com/michaeltyson/TPKeyboardAvoiding (UIScrollView based)
https://github.com/kirpichenko/EKKeyboardAvoiding (UIScrollView based)
https://github.com/robbdimitrov/RDVKeyboardAvoiding (UIScrollView based) https://github.com/hackiftekhar/IQKeyboardManager (looks interesting) https://github.com/danielamitay/DAKeyboardControl (looks interesting)

Author

It'd be great to hear about any cool apps that are using IHKeyboardAvoiding

License

Distributed under the MIT License

Do To:

  • Improve demo project

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.