Reputation: 1204
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;
}
Upvotes: 0
Views: 294
Reputation: 9698
Are you initialising regularView
in viewWillAppear:
or viewDidAppear:
? If that's the case, you should move the initialisation to viewDidLoad
instead.
As you confirmed that it is, the explanation is that viewWillAppear:
might be called many times for the view's lifetime, so it's generally not appropriate to construct UI hierarchy in viewWillAppear:
as you will end up with duplicated view hierarchies.
Upvotes: 1
Reputation: 8608
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 viewDidAppear
method and remove them in your viewDidDissappear
method. 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.
#define kOFFSET_FOR_KEYBOARD 80.0
-(void)keyboardWillShow {
// Animate the current view out of the way
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)keyboardWillHide {
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if ([sender isEqual:mailTf])
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view
CGRect rect = self.view.frame;
if (movedUp)
{
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
rect.size.height += kOFFSET_FOR_KEYBOARD;
}
else
{
// revert back to the normal state.
rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
self.view.frame = rect;
[UIView commitAnimations];
}
- (void)viewWillAppear:(BOOL)animated
{
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
Upvotes: 0
Reputation: 3294
are you using:
-(void) viewWillDisappear:(BOOL)animated {
NSLog (@"Unregister for keyboard events");
[[NSNotificationCenter defaultCenter]
removeObserver:self];
}
if that's not it try doing a check on the location of the view on viewWillAppear.. if it shows the keyboard is up.. dismiss it w/o using animations.
Upvotes: 1