7

对于我为 iPad 设计的应用程序,我有一个滚动视图,其中包含一些文本字段/文本视图。为了保持一切可见,我调整了contentSize滚动视图的属性以在底部添加一个缓冲区,该缓冲区对应于键盘与滚动视图重叠的程度。这是代码(这里有一些特定于应用程序的东西,但希望不要太多以至于你无法理解它):

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [nc addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc removeObserver:self name:nil object:nil];
}

- (void)keyboardWillShow:(NSNotification *)aNotification
{
    NSValue *animationCurve = [[aNotification userInfo] valueForKey:UIKeyboardAnimationCurveUserInfoKey];
    UIViewAnimationCurve curve;
    [animationCurve getValue:&curve];

    NSValue *animationDuration = [[aNotification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval duration;
    [animationDuration getValue:&duration];

    NSValue *endingFrame = [[aNotification userInfo] valueForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect frame;
    [endingFrame getValue:&frame];

    [UIView beginAnimations:@"keyboardWillShow" context:bodyView];
    [UIView setAnimationCurve:curve];
    [UIView setAnimationDuration:duration]; 

    // Re-draw code here.

    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)aNotification
{

    NSValue *animationCurve = [[aNotification userInfo] valueForKey:UIKeyboardAnimationCurveUserInfoKey];
    UIViewAnimationCurve curve;
    [animationCurve getValue:&curve];

    NSValue *animationDuration = [[aNotification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval duration;
    [animationDuration getValue:&duration];

    [UIView beginAnimations:@"keyboardWillHide" context:bodyView];
    [UIView setAnimationCurve:curve];
    [UIView setAnimationDuration:duration];

    // Re-draw code here

    [UIView commitAnimations];      
}

我的问题是:在旋转过程中我如何处理键盘大小?旋转 iPad 时我没有收到任何键盘通知,但键盘的大小发生了显着变化。理想情况下,我只需contentSize通过键盘在横向模式下重叠的量来调整属性的高度,但是如果不对键盘在两个方向上的高度进行硬编码,我就看不到这样做的好方法,我没有不想做。

4

1 回答 1

16

我偶然发现了这个调试其他东西的答案。事实证明,当 iPad 从纵向旋转到横向时,纵向键盘在横向键盘出现(并发送通知)之前隐藏(并发送通知)。因此,只要您考虑到这一点,就可以了。

于 2010-10-04T21:58:21.237 回答