iOS:当键盘可见时,scrollView不会滚动到正确的位置 [英] iOS: scrollView not scrolling to correct position when keyboard visible

查看:283
本文介绍了iOS:当键盘可见时,scrollView不会滚动到正确的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用6.1仿真器测试我的iOS应用.当键盘可见时(在用户单击textView之后),我一直在努力将scrollView滚动到正确的位置.我已尝试遵循此页面上标记为正确的答案:

I am testing my iOS app using a 6.1 simulator. I have been working for hours to scroll my scrollView to the correct position when a keyboard is visible (after a user clicks on a textView). I have tried following the answer marked as correct on this page:

如何在使用键盘时滚动UIScrollView出现吗?

这是我目前拥有的:

- (void)keyboardWasShown:(NSNotification*)aNotification {
    NSLog(@"keyboardWasShown");

    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your app might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, self.activeView.frame.origin) ) {
        NSLog(@"scrollToView");
        CGPoint scrollPoint = CGPointMake(0.0, self.stepDescriptionField.frame.origin.y-kbSize.height);
        NSLog(@"scrollPoint: %f", scrollPoint.y);
        [self.scrollView setContentOffset:scrollPoint animated:YES];
    }

}

从上面的图像中您可以看到,如果用户单击textView,则scrollView不会滚动到正确的位置(您应该能够看到textView的文本内容).

You can see from the images above that if the user clicks on the textView, the scrollView is not scrolled to the correct position (you should be able to see the text contents of the textView).

奇怪的是,我手动尝试将scrollPoint的y-offset更改为不同的值,但它似乎对窗口滚动到的位置没有影响. 我在做什么错了?

The strange thing is that I manually tried changing the y-offset of the scrollPoint to different values, but it seemed to have no effect on where the window scrolls to. What am I doing wrong?

其他可能很重要的事情:

Other things that may be important:

  • 我已关闭自动版式(以便用户可以在此视图中垂​​直滚动).
  • textView不可滚动(已调整大小以适合其内容)

修改

我发现,如果将偏移量添加到contentInsets,如下所示:

I found that if I add my offset to the contentInsets as follows:

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height+50.0, 0.0);

视图将滚动到正确的位置.唯一的缺点是底部有多余的填充物:

the view will scroll to the correct position. The only downside is that there is extra padding at the bottom:

有更好的方法吗?

推荐答案

我将其与UITextField而非UITextView一起使用,但我认为它应该仍能正常工作.这使我可以将文本字段直接定位在键盘上方.

I used this with a UITextField and not a UITextView but i believe it should still work the same. This allows me to positions the textfield directly above the keyboard.

keyboardWillShow是NSNotificationCenter收到UIKeyboardWillShowNotification

keyboardWillShow is the function when NSNotificationCenter receives UIKeyboardWillShowNotification

-(void) keyboardWillShow:(NSNotification *)note
{
// Get the keyboard size
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds];

// Start animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];


// Get Keyboard height and subtract the screen height by the origin of the textbox and height of text box to position textbox right above keyboard
self.scrollView.contentOffset = CGPointMake(0,keyboardBounds.size.height-([UIScreen mainScreen].bounds.size.height - commentBox.frame.origin.y - commentBox.frame.size.height));

[UIView commitAnimations];
}

这篇关于iOS:当键盘可见时,scrollView不会滚动到正确的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆