-
-
Save shepting/6025439 to your computer and use it in GitHub Desktop.
// | |
// 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 |
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.
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. :(
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];
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.
Updated the Gist to use intrinsicContentSize rather than constraints to make even simpler.