Created
November 17, 2015 09:58
-
-
Save kean/511d247fad4d6f76254a to your computer and use it in GitHub Desktop.
UIViewController Keyboard Management
This file contains 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 characters
@interface ViewController : UIViewController | |
@property (nonatomic, readonly, nullable) UIView *keyboardAnchorView; | |
@property (nonatomic, readonly, nullable) NSLayoutConstraint *keyboardAnchorViewHeightConstraint; | |
@end | |
@implementation | |
- (void)viewDidLoad { | |
_keyboardAnchorView = [UIView new]; | |
[self.view addSubview:_keyboardAnchorView]; | |
[_keyboardAnchorView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeTop]; | |
_keyboardAnchorViewHeightConstraint = [_keyboardAnchorView autoSetDimension:ALDimensionHeight toSize:0]; | |
} | |
- (void)viewWillAppear:(BOOL)animated { | |
[super viewWillAppear:animated]; | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; | |
} | |
- (void)viewWillDisappear:(BOOL)animated { | |
[super viewWillDisappear:animated]; | |
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; | |
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; | |
} | |
- (void)keyboardWillShow:(NSNotification *)notification { | |
CGRect keyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; | |
keyboardRect = [self.view convertRect:keyboardRect fromView:[[UIApplication sharedApplication] keyWindow]]; | |
_keyboardAnchorViewHeightConstraint.constant = CGRectGetHeight(self.view.bounds) - keyboardRect.origin.y; | |
[UIView beginAnimations:nil context:NULL]; | |
[UIView setAnimationDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]]; | |
[UIView setAnimationCurve:[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]]; | |
[UIView setAnimationBeginsFromCurrentState:YES]; | |
[self.view layoutIfNeeded]; | |
[[NSNotificationCenter defaultCenter] postNotificationName:ViewControllerKeyboardAnchorViewDidChangeNotification object:self]; | |
[UIView commitAnimations]; | |
} | |
- (void)keyboardWillHide:(NSNotification *)notification { | |
_keyboardAnchorViewHeightConstraint.constant = 0; | |
[UIView beginAnimations:nil context:NULL]; | |
[UIView setAnimationDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]]; | |
[UIView setAnimationCurve:[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]]; | |
[UIView setAnimationBeginsFromCurrentState:YES]; | |
[self.view layoutIfNeeded]; | |
[[NSNotificationCenter defaultCenter] postNotificationName:ViewControllerKeyboardAnchorViewDidChangeNotification object:self]; | |
[UIView commitAnimations]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment