Skip to content

Instantly share code, notes, and snippets.

@shepting
Last active March 22, 2016 02:50
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save shepting/6025439 to your computer and use it in GitHub Desktop.
Save shepting/6025439 to your computer and use it in GitHub Desktop.
A great little helper for handling keyboard animations nicely. Just put this view at the bottom vertically of your views and it will move everything else up and down for you.
//
// YMKeyboardLayoutHelperView.m
// ios-chat
//
// Created by Steven Hepting on 7/17/13.
// Copyright (c) 2013 Yammer. All rights reserved.
//
#import "YMKeyboardLayoutHelperView.h"
#import "UIView+LayoutAdditions.h"
@interface YMKeyboardLayoutHelperView ()
@property (nonatomic) CGFloat desiredHeight;
@property (nonatomic) CGFloat duration;
@end
@implementation YMKeyboardLayoutHelperView
- (id)init
{
self = [super init];
if (self) {
self.translatesAutoresizingMaskIntoConstraints = NO;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
return self;
}
- (void)keyboardWillShow:(NSNotification *)notification
{
// Save the height of keyboard and animation duration
NSDictionary *userInfo = [notification userInfo];
CGRect keyboardRect = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
self.desiredHeight = CGRectGetHeight(keyboardRect);
self.duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
[self animateSizeChange];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
self.desiredHeight = 0.0f;
[self animateSizeChange];
}
- (CGSize)intrinsicContentSize
{
return CGSizeMake(UIViewNoIntrinsicMetric, self.desiredHeight);
}
- (void)animateSizeChange
{
[self invalidateIntrinsicContentSize];
// Animate transition
[UIView animateWithDuration:self.duration animations:^{
[self.superview layoutIfNeeded];
}];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
@shepting
Copy link
Author

Updated the Gist to use intrinsicContentSize rather than constraints to make even simpler.

@garfbargle
Copy link

What is the #import "UIView+LayoutAdditions.h" for? I can't seem to get this to work :( I am placing a view in my storyboard and making this class the custom class. I moved your init code into my initFromCoder and it will not resize the view.

@garfbargle
Copy link

I was able to get it working correctly, works awesome when I use pure code but I'm having a lot of trouble with using it in the interface builder. :(

@mpvosseller
Copy link

So elegant! One small bug I noticed when running on iPad and with rotation is that you need to convert the keyboard rect from screen coordinates (as delivered in the notification) to view coordinates. Otherwise width / height get mixed up. I'm using something like this:

CGRect screenRect = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect keyboardRect = [self convertRect:screenRect fromView:nil];

@shepting
Copy link
Author

This piece of code has been integrated into a little more full-featured set of autolayout bits here: https://github.com/shepting/PSAutolayout

The rotation bug that mpvosseller mentioned is fixed in that version as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment