Created
April 11, 2014 13:21
-
-
Save gwennguihal/10468345 to your computer and use it in GitHub Desktop.
KeyBoard Handler IOS
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
| - (void)setupKeyboard | |
| { | |
| [[NSNotificationCenter defaultCenter] addObserver:self | |
| selector:@selector(keyboardWasShown:) | |
| name:UIKeyboardWillShowNotification object:nil]; | |
| [[NSNotificationCenter defaultCenter] addObserver:self | |
| selector:@selector(keyboardWillBeHidden:) | |
| name:UIKeyboardWillHideNotification object:nil]; | |
| UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] | |
| initWithTarget:self | |
| action:@selector(dismissKeyboard)]; | |
| [self addGestureRecognizer:tap]; | |
| } | |
| -(void)clean | |
| { | |
| [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
| } | |
| // Called when the UIKeyboardDidShowNotification is sent. | |
| - (void)keyboardWasShown:(NSNotification*)aNotification | |
| { | |
| NSDictionary* info = [aNotification userInfo]; | |
| CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; // keyboard size | |
| keyBoardHeight = kbSize.height; | |
| CGSize screenSize = [UIScreen mainScreen].bounds.size; | |
| CGRect frame = [_scrollView convertRect:_scrollView.bounds toView:self.superview]; | |
| UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height - (screenSize.height - frame.origin.y - frame.size.height), 0.0); | |
| self.scrollView.contentInset = contentInsets; | |
| self.scrollView.scrollIndicatorInsets = contentInsets; | |
| [self scrollToSeeTextField]; | |
| } | |
| // Called when the UIKeyboardWillHideNotification is sent | |
| - (void)keyboardWillBeHidden:(NSNotification*)aNotification | |
| { | |
| keyBoardHeight = 0; | |
| UIEdgeInsets contentInsets = UIEdgeInsetsZero; | |
| self.scrollView.contentInset = contentInsets; | |
| self.scrollView.scrollIndicatorInsets = contentInsets; | |
| } | |
| -(void)dismissKeyboard | |
| { | |
| [activeTextField resignFirstResponder]; | |
| } | |
| - (IBAction)textFieldEditBegin:(id)sender | |
| { | |
| activeTextField = sender; | |
| [self scrollToSeeTextField]; | |
| } | |
| - (IBAction)textFieldEditEnd:(id)sender | |
| { | |
| activeTextField = nil; | |
| } | |
| -(void)scrollToSeeTextField | |
| { | |
| if (keyBoardHeight > 0) | |
| { | |
| CGRect aRect = self.bounds; | |
| aRect.size.height -= keyBoardHeight; | |
| CGRect frame = [activeTextField convertRect:activeTextField.bounds toView:self]; | |
| if (!CGRectContainsPoint(aRect, frame.origin) ) | |
| { | |
| frame = [self convertRect:frame toView:self.scrollView]; | |
| [self.scrollView scrollRectToVisible:frame animated:YES]; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment