Skip to content

Instantly share code, notes, and snippets.

@NikKovIos
Created April 2, 2018 17:25
Show Gist options
  • Save NikKovIos/a3a9b61a78caac75b665ae1c1982cbbf to your computer and use it in GitHub Desktop.
Save NikKovIos/a3a9b61a78caac75b665ae1c1982cbbf to your computer and use it in GitHub Desktop.
Keyboard handling when it overlaps a TextField. Without scroll view.
@interface KeyboardTextField()
@end
@implementation KeyboardTextField
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillShow:(NSNotification*)aNotification
{
CGFloat keyboardHeight = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
[UIView animateWithDuration:0.2 animations:^
{
CGRect alteredFrame = [self.view frame];
alteredFrame.origin.y = -keyboardHeight;
[self.view setFrame:alteredFrame];
} completion:nil];
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
[UIView animateWithDuration:0.2 animations:^ {
CGRect alteredFrame = [self.view frame];
alteredFrame.origin.y = 0;
[self.view setFrame:alteredFrame];
} completion:nil];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment