【问题标题】:Why does my screen scroll back down after I scroll it up?为什么我的屏幕向上滚动后又向下滚动?
【发布时间】:2012-06-23 01:53:48
【问题描述】:

我在 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];
        } 
}

【问题讨论】:

    标签: objective-c ios keyboard scroll


    【解决方案1】:

    您是否没有更改某些文本字段委托方法的其他内容?如果没有,您似乎没有正确设置滚动视图的内容大小,因此在 setContentOffset:Animated: 之后,滚动视图只会自行调整。

    【讨论】:

    • 文档的编写方式告诉我,滚动视图不会自行调整,也没有任何自动行为。但是随着我更多地使用该应用程序,我似乎只在 TextViews 上获得了这种捕捉行为。 ScrollView 会自动调整自身,以便当前正在编辑的 TextView 的行保持在键盘的顶部。每次光标移动到新行时,ScrollView 都会向上滚动一行以使光标保持在视图中。这没关系,只是不是我想要的,也没有在我正在阅读的 Apple 文档中描述。我想关闭此行为。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    相关资源
    最近更新 更多