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

发布于 2024-09-26 10:00:48 字数 2393 浏览 6 评论 0原文

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

For an app I’m designing for the iPad, I have a scroll view with some text fields/text views in it. In order to keep everything visible, I adjust the contentSize property of the scroll view to add a buffer at the bottom that corresponds to how much the keyboard overlaps the scroll view. Here’s the code (there’s some app-specific stuff here, but hopefully not so much that you can’t make sense of it):

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

My question is this: what do I do about keyboard size during rotation? I don’t receive any keyboard notifications when the iPad is rotated, but the size of the keyboard changes significantly. Ideally I’d simply adjust the height of the contentSize property by the amount the keyboard overlaps in landscape mode, but I can't see a good way to do that without hard-coding the height of the keyboard in both orientations, which I don’t want to do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望笑 2024-10-03 10:00:48

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

I accidentally found out the answer to this debugging something else. It turns out that when the iPad rotates from portrait to landscape, the portait keyboard hides (and sends its notification) just before the landscape keyboard appears (and sends its notification). So as long as you account for that, you’re OK.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文