The simple way to move UITextfield on top of UIKeyBoard is to
1)Put your UIView inside UIScrollView
2)Register for keyboard notification
3)In Keyboard event find keyboard size and scroll up if UITextField present below the UIKeyboard
1)Put your UIView inside UIScrollView
2)Register for keyboard notification
3)In Keyboard event find keyboard size and scroll up if UITextField present below the UIKeyboard
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
//kbSize.height and kbSize.width vary in Orientation
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height+(activeField.frame.size.height*2); // add textfield height when the UITextField slightly outside from keyboard view
if (!CGRectContainsPoint(aRect, activeField.frame.origin) )
{
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y- kbSize.height);
[scrollView setContentOffset:scrollPoint animated:YES];
}
scrollView.contentSize = self.view.frame.size;
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
scrollView.contentOffset = CGPointZero;
}
Download the source code : Move-UITextField-Top-UIKeyBoard
No comments:
Post a Comment