Reputation: 295
I am getting problem when trying to move my scroll view up when the keyboard appears. The scroll view moves up in ios7 but in ios6 it doesn't and there is additional white space above the keyboard that hides the controls on the screen
my code:
- (void)viewWillAppear:(BOOL)animated
{
[super viewDidAppear:animated];
keyboardIsShown = NO;
[super viewWillAppear:animated];
[[self view] endEditing:YES];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:self.view.window];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:self.view.window];
}
- (void)keyboardWillShow:(NSNotification *)n
{
if (keyboardIsShown) {
return;
}
NSDictionary* userInfo = [n userInfo];
// get the size of the keyboard
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// resize the noteView
CGRect viewFrame = self.scrollView.frame;
viewFrame.size.height -= (keyboardSize.height - kTabBarHeight);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
// The kKeyboardAnimationDuration I am using is 0.3
[UIView setAnimationDuration:kKeyboardAnimationDuration];
[self.scrollView setFrame:viewFrame];
[UIView commitAnimations];
scrollView.contentSize = formView.frame.size;
keyboardIsShown = YES;
}
Whats the problem here. pls help
Upvotes: 1
Views: 964
Reputation: 5368
The problem is, that you only change size of the scroll view, but you don't tell the scrollview to scroll to the text field.
Look at my method scrollToEditingTextField for better understanding.
- (void)keyboardWillShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize beginSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// keyboard will appear
if(!_keyboardShowed) {
_keyboardShowed = YES;
[UIView animateWithDuration:0.4
animations:^{
CGFloat scrollHeight =_scrollView.size.height - (IS_PORTRAIT_ORIENTATION ? beginSize.height : beginSize.width);
CGRect scrollFrame = _scrollView.frame;
scrollFrame.size.height = scrollHeight;
_scrollView.frame = scrollFrame;
}
completion:^(BOOL finished){ }];
}
[self scrollToEditingTextField];
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize endSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
// keyboard will disappear
if(_keyboardShowed) {
CGFloat scrollHeight =_scrollView.size.height + (IS_PORTRAIT_ORIENTATION ? endSize.height : endSize.width);
CGRect scrollFrame = _scrollView.frame;
scrollFrame.size.height = scrollHeight;
_scrollView.frame = scrollFrame;
_keyboardShowed = NO;
}
}
-(void)scrollToEditingTextField {
// find which text field is currently editing
UITextField *editingTextFiled = [self editingTextField:self.thisView];
if(editingTextFiled == nil) return; // text field didnt found
CGPoint scrollPoint = [editingTextFiled convertPoint:CGPointMake(0, 0) toView:self.scrollView];
scrollPoint.y -= 70;
scrollPoint.x = 0;
[_scrollView setContentOffset:scrollPoint animated:YES];
}
-(UITextField*)editingTextField:(UIView*)view
{
if( ( [[view class] isSubclassOfClass:[UITextField class]] ||
[[view class] isSubclassOfClass:[UITextView class]] ) &&
[view isFirstResponder] ) {
return (UITextField*)view;
}
for(UIView *subview in view.subviews ) {
if([[subview class] isSubclassOfClass:[UISearchBar class]]) {
return nil;
}
if( ( [[subview class] isSubclassOfClass:[UITextField class]] ||
[[subview class] isSubclassOfClass:[UITextView class]] ) &&
[subview isFirstResponder] ) {
return (UITextField*)subview;
}
}
// recursion
for(UIView *subview in view.subviews ) {
UITextField *textField = [self editingTextField:subview];
if(textField) return textField;
}
return nil;
}
Upvotes: 1