4

我有一个滚动视图,上面有多个视图。所有这些视图上都有文本字段,一旦单击,键盘就会弹出。在某些情况下,键盘可能会隐藏这个子视图,我想确定这是否可以提前计算出来。如果是的话,我该怎么做..?scrollRectToVisibleof方法可以UIScrollView在这里用于任何用途.. 吗?您可以查看随附的图片以进行进一步说明。感谢您的任何想法..

编辑: 这些子视图是动态绘制的,所以我无法确定和硬编码。

在此处输入图像描述

4

3 回答 3

4

在横向模式下:

iPad 高度:768 像素。

键盘高度:352 像素。

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

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

于 2013-02-28T10:05:57.747 回答
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.

}
于 2013-02-28T10:38:52.127 回答
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"];
}
于 2013-02-28T10:09:37.927 回答