旋转后获得iPad屏幕键盘的大小

时间:2010-10-04 21:31:22

标签: ipad uikeyboard ios

对于我正在为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属性的高度,但如果不在两个方向上对键盘的高度进行硬编码,我看不到一个好的方法,我不想这样做。

1 个答案:

答案 0 :(得分:16)

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