Skip to content

Instantly share code, notes, and snippets.

@kean
Created November 17, 2015 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kean/511d247fad4d6f76254a to your computer and use it in GitHub Desktop.
Save kean/511d247fad4d6f76254a to your computer and use it in GitHub Desktop.
UIViewController Keyboard Management
@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