0

これが何が起こっているかのスクリーンショットです。

緑の背景は、スクロールビューのビューです(つまり、「コンテンツサイズ」と「コンテンツオフセット」をマークします)。

オレンジはスクロールビューフレームです。

キーボードが上がっています...スクロールビューでコンテンツがうまくスクロールされました。

ここに画像の説明を入力してください

テキストフィールドをクリックすると、キーボードが非表示になります

ここに画像の説明を入力してください

スクロールビュー(オレンジ)は適切な高さのように見えますが、コンテンツは約100px(緑)にジャンプしています。

何がこれを引き起こしているのかわかりません-それがiOSのバグなのか、それとも何なのか(私のデバイスでも起こります)。

これは、キーボードが表示/非表示になるタイミングに基づいてビューのサイズを変更するコードです。

- (void)keyboardWillShow:(NSNotification *)notif {
    [self.mainScrollView setAutoresizesSubviews:NO];
    // get the scroll view frame size
    CGRect frame = [self.mainScrollView frame];
    frame.size.height = self.view.frame.size.height-216;

    [UIView beginAnimations:@"scrollViewAnimations" context:nil];
    [UIView setAnimationDuration:0.3];
    [self.mainScrollView setFrame:frame];
    [UIView commitAnimations];

    // add tap gesture recognizer
    _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_dismissKeyboard:)];
    [_tap setDelegate:self];
    [self.view addGestureRecognizer:_tap];
}

- (void)keyboardWillHide:(NSNotification *)notif {
    CGRect frame = [self.mainScrollView frame];
    frame.size.height = self.view.frame.size.height;

    [UIView beginAnimations:@"scrollViewAnimations" context:nil];
    [UIView setAnimationDuration:0.3];
    [self.mainScrollView setFrame:frame];
    [UIView commitAnimations];

    [self.view removeGestureRecognizer:_tap];
}

どんな助けやガイダンスも素晴らしいでしょう、ありがとう!なぜこれをしているのですか?それはバグですか?それは私のコードのどこかに埋もれているものですか?もしそうなら、私はそれを見つけることができません。これは、ビューアニメーションの実行方法のエラーですか?

4

1 に答える 1

5

RubyMotionの同様の問題に対する私の解決策は次のとおりです。

def keyboardWillShow(notification)
  point = searchBar.convertPoint(searchBar.frame.origin, toView:tableView)
  tableView.setContentOffset(point, animated: true)
end

def keyboardWillHide(notification)
  info = notification.userInfo
  duration = info.objectForKey(UIKeyboardAnimationDurationUserInfoKey)

  UIView.beginAnimations(nil, context:nil)
  UIView.setAnimationDuration(duration)
  tableView.setContentOffset([0,0], animated: false)
  UIView.commitAnimations
end

ここでの考え方は、フレームを実際に変更したくないので、代わりにUIScrollViewのコンテンツオフセットを調整したいということです。

于 2012-11-06T16:46:53.120 に答える