Created
April 2, 2018 17:25
-
-
Save NikKovIos/a3a9b61a78caac75b665ae1c1982cbbf to your computer and use it in GitHub Desktop.
Keyboard handling when it overlaps a TextField. Without scroll view.
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 characters
@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