处理键盘显示/隐藏&方向变化

时间:2011-03-04 16:17:21

标签: iphone objective-c ipad ios

我在键盘显示/隐藏后使用以下方法调整视图大小:

- (void)moveViewForKeyboard:(NSNotification*)aNotification up:(BOOL)up {

   NSDictionary* userInfo = [aNotification userInfo];

   // Get animation info from userInfo
   NSTimeInterval animationDuration;
   UIViewAnimationCurve animationCurve;
   CGRect keyboardEndFrame;
   [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
   [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
   [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];

   // Animate up or down
   [UIView beginAnimations:nil context:nil];
   [UIView setAnimationDuration:animationDuration];
   [UIView setAnimationCurve:animationCurve];

   CGRect newFrame = self.view.frame;
   CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];

   newFrame.size.height += (up? -1 : 1) * keyboardFrame.size.height;

   self.view.frame = newFrame;

   keyboardUp = up;

   [UIView commitAnimations];
}

这非常有效,直到屏幕方向改变。然后会发生的方法是该方法正确调整视图大小,但在此方法返回后 - 其他内容会再次将视图调整为屏幕的最大高度。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

首先,您需要弄清楚再次调整视图的大小。我建议在上面的方法中加一个断点,这样当它被调用第二个不需要的时间时,你可以查看方法调用堆栈,看看是什么导致了第二次调用。然后,您将能够阻止它发生,或者更改您的代码,以免它导致问题。

相关问题