Last active
March 22, 2016 02:50
-
-
Save shepting/6025439 to your computer and use it in GitHub Desktop.
Revisions
-
shepting revised this gist
Jul 18, 2013 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,8 +22,8 @@ - (id)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; } @@ -32,9 +32,9 @@ - (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]; } @@ -66,4 +66,4 @@ - (void)dealloc [[NSNotificationCenter defaultCenter] removeObserver:self]; } @end -
shepting revised this gist
Jul 18, 2013 . 1 changed file with 10 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -36,40 +36,34 @@ - (void)keyboardWillShow:(NSNotification *)notification 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 created this gist
Jul 17, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,75 @@ // // 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[@"UIKeyboardBoundsUserInfoKey"] CGRectValue]; self.desiredHeight = CGRectGetHeight(keyboardRect); self.duration = [userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue]; [self setNeedsUpdateConstraints]; } - (void)keyboardWillHide:(NSNotification *)notification { // Reset the desired height (keep the duration) self.desiredHeight = 0.0f; [self setNeedsUpdateConstraints]; } - (void)updateConstraints { [super updateConstraints]; // Remove old constraints if ([self.constraints count]) { [self removeConstraints:self.constraints]; } // Add new constraint with desired height NSString *constraintFormat = [NSString stringWithFormat:@"V:[self(%f)]", self.desiredHeight]; [self addVisualConstraints:constraintFormat views:@{@"self": self}]; // Animate transition [UIView animateWithDuration:self.duration animations:^{ [self.superview layoutIfNeeded]; }]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } @end