如何确定键盘是否隐藏了子视图......?

时间:2013-02-28 09:57:22

标签: iphone ios objective-c uiscrollview keyboard

我有一个包含多个视图的scrollview。所有这些视图都有文本字段,一旦点击弹出键盘。在某些情况下,键盘可能会隐藏此子视图,我想确定是否可以提前计算。如果是,我怎么能这样做..? scrollRectToVisible方法的UIScrollView可以在这里用于任何用途..?你看一下附图,以便进一步澄清。谢谢你的任何想法..

修改 这些子视图是动态绘制的,所以我无法确定并硬编码。

enter image description here

3 个答案:

答案 0 :(得分:4)

在横向模式中:

iPad的高度:768像素。

键盘高度:352像素。

这意味着键盘x坐标:0.0和y坐标:416

每当你的控件y坐标大于416时。理解它会在弹出键盘时被隐藏。

答案 1 :(得分:1)

是的,您需要稍微调整子视图以使其可见,您可以使用此动画将其移动到任何点。只需传递图层和目标点。一旦用户按下键盘上的返回键,再将其移至上一点:)

-(void)moveLayer:(CALayer*)layer to:(CGPoint)point
{
    // Prepare the animation from the current position to the new position
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.fromValue = [layer valueForKey:@"position"];
    animation.duration = 0.2;

    animation.toValue = [NSValue valueWithCGPoint:point];

    // Update the layer's position so that the layer doesn't snap back when the animation completes.
    layer.position = point;

    // Add the animation, overriding the implicit animation.
    [layer addAnimation:animation forKey:@"position"];
}

答案 2 :(得分:1)

注册以下通知:

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidChangeFrame:)
                                                 name:UIKeyboardDidChangeFrameNotification
                                               object:nil];

请务必妥善处理:

- (void)keyboardDidChangeFrame:(NSNotification *)notification
{
    CGRect keyboardEndFrame;
    [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
    CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame fromView:nil];

    // Add your code to check if the keyboard is hiding your views.
    // CGRectIntersectsRect should help you here.
    // For each view you are worried about hiding, run CGRectIntersectsRect to check
    // if an intersection occurs. If it does, then you can
    // move your view using UIScrollView's setContentOffset: animated: method.

}