滚动后,为什么我的屏幕会向下滚动?

时间:2012-06-20 20:41:08

标签: objective-c ios keyboard scroll

我在iPad应用中滚动屏幕时遇到问题。在我的应用程序中,页面底部附近可能有很多字段。因此,不是根据当前活动字段的位置频繁调整屏幕位置,我希望屏幕处于两个位置之一:滚动或不滚动。如果用户正在操作任何部分将被键盘隐藏的字段,我想按键盘的高度向上滚动屏幕。

我的问题是,在键盘出现并且屏幕滚动了我的代码指定的距离之后,屏幕然后快速向下捕捉到一个位置,使得光标,无论它碰巧在哪里,都在键盘的顶部上方。我不明白是什么导致了最后的瞬间。我的滚动代码(从教程中提取)如下所示。

由于

//Scroll the screen up if the keyboard hides the current field
- (void)keyboardDidShow:(NSNotification *)notification
{
    // Step 1: Get the height of the keyboard.
    if ([self interfaceOrientation] == UIInterfaceOrientationPortrait || [self interfaceOrientation] == UIInterfaceOrientationPortraitUpsideDown) 
    {
        keyboardHeight = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    }
    else 
    {
        keyboardHeight = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.width;
    }

    // Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight, 0.0);
    [(UIScrollView*)[self view] setContentInset: contentInsets];
        [(UIScrollView*)[self view] setScrollIndicatorInsets: contentInsets];

    // Step 3: Scroll the screen up by the height of the keyboard.
        visibleRect = self.view.frame;
        visibleRect.size.height -= (keyboardHeight + [[self toolbar] bounds].size.height);
        if (([self.activeField frame].origin.y + [self.activeField frame].size.height) > visibleRect.size.height) 
        {
            //make sure scrolling is vertical only
            CGPoint scrollPoint = CGPointMake(currentLeftEdge, keyboardHeight);
            [(UIScrollView*)[self view]  setContentOffset: scrollPoint animated:YES];
        } 
}

1 个答案:

答案 0 :(得分:0)

您是否在某些textfield委托方法上更改了其他内容?如果没有,看起来你没有正确设置scrollview的内容,所以在setContentOffset:Animated:之后,scrollview只是调整自己。