1

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

1 に答える 1

0

テキストフィールドのデリゲート メソッドのいくつかで他に何か変更していませんか? そうでない場合は、スクロールビューのコンテンツサイズを適切に設定していないようです。そのため、setContentOffset:Animated: の後、スクロールビューは自動的に調整されます。

于 2012-06-20T21:03:31.030 に答える