GithubHelp home page GithubHelp logo

dustinsenos / lhskeyboardadjusting Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lionheart/lhskeyboardadjusting

0.0 0.0 0.0 103 KB

An easy-to-use utility that will automatically resize / adjust views whenever a keyboard appears.

License: Apache License 2.0

Ruby 14.74% Objective-C 85.26%

lhskeyboardadjusting's Introduction

LHSKeyboardAdjusting

After one too many times of referencing a Gist I wrote on how to set up automatic view resizing when a keyboard appears in iOS, I decided to just abstract the entire thing and throw it up on GitHub.

LHSKeyboardAdjusting will adjust the bottom position of your UIScrollView or UITableView when a keyboard appears on screen using Auto Layout. All you have to do is provide LHSKeyboardAdjusting with an NSLayoutConstraint that pins the bottom of your view to the bottom of the screen. LHSKeyboardAdjusting will automatically adjust that constraint and pin it to the top of the keyboard when it appears.

Note: LHSKeyboardAdjusting requires Auto Layout in your build target, so it will only work with iOS 6+.

Installation

LHSKeyboardAdjusting uses Cocoapods, so in your Podfile, just add something like this:

pod 'LHSKeyboardAdjusting', '0.0.1'

Then run pod update and you'll be ready to go.

If you don't use Cocoapods, dragging and dropping the LHSKeyboardAdjusting folder into your Xcode project will do the trick as well.

Usage

  1. In your view controller's header file, import and assign the LHSKeyboardAdjusting protocol.

    #import <LHSKeyboardAdjusting/LHSKeyboardAdjusting.h>
    
    @interface AViewController : UIViewController <LHSKeyboardAdjusting>
  2. Figure out which view you'd like to pin to the top of the keyboard. It could be anything, but a UIScrollView, UITableView, or a UITextView are the most likely candidates. Then, wherever you're setting up your view constraints, define an NSLayoutConstraint property that pins the bottom of this view to the bottom of the screen, like so:

    self.bottomConstraint = [NSLayoutConstraint constraintWithItem:self.adjustingView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.bottomLayoutGuide attribute:NSLayoutAttributeTop multiplier:1 constant:0];
    [self.view addConstraint:self.bottomConstraint];

    After you've done this, define the keyboardAdjustingBottomConstraint delegate method to return this constraint:

    #pragma mark - LHSKeyboardAdjusting
    
    - (NSLayoutConstraint *)keyboardAdjustingBottomConstraint {
         return self.bottomConstraint;
    }
  3. All you need to do now is activate and deactivate the automatic adjustments.

    #import <LHSKeyboardAdjusting/UIViewController+LHSKeyboardAdjustment.h>
    
    // other stuff here
    
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        [self lhs_activateKeyboardAdjustment];
    }
    
    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        [self lhs_deactivateKeyboardAdjustment];
    }
  4. And you're done! Whenever a keyboard appears, your views will be automatically resized.

Contributing

The only big feature I see missing is iOS 5 support, but otherwise I don't think much else is needed for this project. If you see a bug, please file an issue and I'll look into it right away.

Support

If you like this library, consider supporting my open source efforts through Gittip.

License

Apache 2.0, see LICENSE file for details.

lhskeyboardadjusting's People

Contributors

dlo avatar dustinsenos 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.