I have a function in my app which moves the view up, whenever the keyboard is displayed. Unfortunately there is a bug; the first time you load the view everything works fine, but if you switch to another view, and then switch back, the view no longer moves 🙁
I added some NSLog statements to my code to try and trace the problem. I am using NSNotification, and that is working fine because the method gets called every time.
Then I thought maybe it was a problem with the coordinates of the view, so I added statements that printed out the origin of the view. They print out the correct origin (the ‘moved’ origin), even though the view definitely didn’t move.
So it seems that the Xcode thinks that it has moved the view, but it hasn’t. Has anyone else encountered this behaviour ?
EDIT: here is some code
Setting up the notifications:
//register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:self.view.window];
//if the keyboard is already being shown because someone was entering a comment, and then they switch to a textfield, this will move the view back down.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UITextFieldTextDidBeginEditingNotification object:self.view.window];
//hide the keyboard
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:self.view.window];
//hide the keyboard if we're done with the textview
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UITextViewTextDidEndEditingNotification object:self.view.window];
keyboardIsShown = FALSE;
tempDelegate.keyboardIsInitialized = TRUE;
the method to show the keyboard and move the view:
-(void)keyboardWillShow:(NSNotification *)notif{
NSLog(@"keyboardWillShow");
NSLog(@"type: %@, keyboardIsShown: %@", sender, keyboardIsShown);
//double check
if (keyboardIsShown || !sender) {
NSLog(@"return");
return;
}
//only adjust screen for comment box (which is a textview)
if(![sender isEqualToString:@"text field"] && [sender isEqualToString:@"text view"]){
NSLog(@"if");
NSDictionary* userInfo = [notif userInfo];
// get the size of the keyboard
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
[UIView beginAnimations:@"ResizeForKeyboard" context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
NSLog(@"regular BEFORE: %@", NSStringFromCGRect(regularView.frame));
regularView.frame = CGRectMake(0, - keyboardSize.height, CGRectGetWidth(imageView.bounds)*scrollView.zoomScale, CGRectGetHeight(imageView.bounds)*scrollView.zoomScale);
NSLog(@"regular AFTER: %@", NSStringFromCGRect(regularView.frame));
[UIView commitAnimations];
keyboardIsShown = YES;
}
}
and the method to hide the keyboard and move the view back:
-(void)keyboardWillHide:(NSNotification *)notif{
NSLog(@"keyboardWillHide");
if (!keyboardIsShown) {
NSLog(@"return");
return;
}
[UIView beginAnimations:@"ResizeForKeyboard" context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
NSLog(@"regular BEFORE: %@", NSStringFromCGRect(regularView.frame));
self.regularView.frame = CGRectMake(0, 0, CGRectGetWidth(imageView.bounds)*scrollView.zoomScale, CGRectGetHeight(self.imageView.bounds)*scrollView.zoomScale);
[UIView commitAnimations];
NSLog(@"regular AFTER: %@", NSStringFromCGRect(regularView.frame));
keyboardIsShown = NO;
}
Are you adding or removing the NSNotification at any point?
Also, can you post your code for how your moving your view? There maybe some issue there.
EDIT:
So from what it seems is that your notifications are being removed at some point and not being added back.
Move the notification init into your
viewDidAppearmethod and remove them in yourviewDidDissappearmethod. This will ensure that whenever your view appears or disappears notifications are added or removed respectively.What you are probably doing is adding them in your viewDidLoad, which this method only gets called when the view is first loaded (so only once). So if you push a new view then pop back, they could get removed and not added back.
EDIT:
Here is a chuck of code that I usually use when I need to animate with a keyboard.