我的应用程序中有一个函数,当键盘显示时,视图向上移动。不幸的是有一个错误;您第一次加载视图一切工作正常,但如果您切换到另一个视图,然后切换回来,视图不再移动:(奇怪的视图行为
我添加了一些NSLog
语句到我的代码来尝试和追踪问题。我正在使用NSNotification,而且工作正常,因为每次都调用方法。
然后我想也许这是一个视图坐标问题,所以我添加了打印出视图原点的语句。他们打印出正确的来源('移动'的起源),尽管视图肯定没有移动。
因此,看起来Xcode认为它已经移动了视图,但它没有。有其他人遇到过这种行为吗?
编辑:这里是一些代码
设置的通知:
//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;
的方法来显示键盘和移动视图:
-(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;
}
}
和方法隐藏键盘并将视图移回:
-(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;
}
您是否在任何时候添加或删除NSNotification? 此外,你可以发布你的代码如何移动你的观点?那里可能有一些问题。 – random 2012-08-07 17:49:03
我删除dealloc和viewDidUnload中的通知...我在代码中编辑到OP – BloonsTowerDefence 2012-08-07 17:55:37