Skip to content

Instantly share code, notes, and snippets.

@dfjdejulio
Last active August 29, 2015 14:01
Show Gist options
  • Save dfjdejulio/6d05728e3fcf225dd394 to your computer and use it in GitHub Desktop.
Save dfjdejulio/6d05728e3fcf225dd394 to your computer and use it in GitHub Desktop.
Handling iOS Keyboard Show/Hide for Dummies

Handling iOS Keyboard Show/Hide for Dummies

  1. Use auto layout in your nib/xib/storyboard.
  2. Have precisely one constraint be glued to the bottom guide, and make it the highest priority.
  3. Add this class to your project.
  4. Change the class of that one special constraint to match this class.

Now, whenever the keyboard shows or hides or changes height (for example when switching between English and Japanese), the rest of your layout will automatically get out of the way.

How it works: every time the top of the keyboard moves up or down, the distance encoded in this constriant changes by the exact same amount. There's no need to initialize it or anything, it registers for the right events all by itself on being loaded from the nib/xib/storyboard. Just make sure there's only one instance of this class, and it's used by the one constraint that's glued to the bottom guide, and that you have all your other bottom-related constraints cascade from that, and it will all just work.

//
// KBBLayoutConstraint.h
// Runner
//
// Created by Doug DeJulio on 5/17/14.
// Copyright (c) 2014 AISB. All rights reserved.
//
#import <UIKit/UIKit.h>
/**
The way you use this is, in your storyboard, have precisely one vertical
NSLayoutConstraint that touches the bottom layout guide. Change the
class of that constraint to KBBlayoutConstraint. When the storyboard is
loaded, the "awakeFromNib" method will register it for keyboard show/hide
events. The "size" of the constraint will change by however much the top
of the keyboard changed. Magic!
*/
@interface KBBLayoutConstraint : NSLayoutConstraint
#pragma mark Keyboard Handling
- (void) keyboardWillChange: (NSNotification *)notification;
#pragma mark Lifecycle
- (void)awakeFromNib;
@end
//
// KBBLayoutConstraint.m
// Runner
//
// Created by Doug DeJulio on 5/17/14.
// Copyright (c) 2014 AISB. All rights reserved.
//
#import "KBBLayoutConstraint.h"
@implementation KBBLayoutConstraint
- (void) awakeFromNib
{
[super awakeFromNib];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillHideNotification object:nil];
}
#pragma mark Keyboard Handling
- (void) keyboardWillChange: (NSNotification *)notification
{
CGRect rawBeforeRect = [notification.userInfo[@"UIKeyboardFrameBeginUserInfoKey"] CGRectValue];
CGRect rawAfterRect = [notification.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
CGRect beforeRect = [self.firstItem convertRect: rawBeforeRect fromView:nil];
CGRect afterRect = [self.firstItem convertRect: rawAfterRect fromView:nil];
// How far did the top of the keyboard move?
CGFloat deltaY = beforeRect.origin.y - afterRect.origin.y;
// Adjust the bottom constraint by that much.
self.constant += deltaY;
return;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment