2011-03-04 67 views
0

我用下面的方法来调整键盘显示/隐藏后的视图:处理键盘的显示/隐藏和方向改变

- (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]; 
} 

这个作品非常好,直到屏幕方向的变化。然后会发生的是该方法正确调整视图的大小,但在此方法返回后 - 再次将视图的大小重新调整为屏幕的最大高度。

任何想法?

回答

0

首先你需要弄清楚什么是再次调整视图大小。我建议在上述方法中添加一个断点,以便在被调用第二个不需要的时间时,可以查看方法调用堆栈并查看导致第二个调用的是什么。然后你就可以阻止它的发生,或者改变你的代码,这样它就不会引起问题。

+0

问题在于,我无法找到重新调整大小的原因和位置。 – 2011-03-04 21:30:03

+0

您是否设置了中断点并查看了调用堆栈?您的方法是第二次调用还是其他代码? – 2011-03-04 21:37:08