UIWebView 在键盘出现时滚动.导致点击偏移错误

UIWebView scrolling when keyboard appears. Causing tap offset bug?

本文关键字:错误 滚动 键盘 UIWebView      更新时间:2023-09-26

我有一个UIWebView,其中包含需要用户输入iPad应用程序的登录字段。视图以模式显示,当横向显示时,键盘显示,这会导致UIWebView向上滚动。

这是错误发生的地方 - UIWebView包含一个点击延迟,我还没有找到可靠的解决方案。(我正在使用OAuth所以任何Javascript注入都无法可靠地工作)。显示键盘,因此当我点击输入字段时,点击会在视图自动滚动并且不在正确的位置后注册。

从本质上讲,这就是我点击顶部输入字段,并且点击记录的比应有的低约 20px,因为视图正在被键盘移动。

我尝试过防止UIWebView滚动,但无论如何键盘总是让它移动。我尝试注入Javascript以消除点击延迟,但也没有成功。

任何帮助不胜感激!

我正在努力解决同样的问题。我还没有解决抽头偏移问题,但就关闭抽头延迟而言,您可以使用:

[[self.webView scrollView] setDelaysContentTouches:NO];

不确定您是否已经找到此页面,但您可以为键盘通知添加侦听器并自己操作滚动。这是苹果的文档链接:https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

以下是链接中与操作视图相关的代码:

// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(keyboardWasShown:)
        name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
         selector:@selector(keyboardWillBeHidden:)
         name:UIKeyboardWillHideNotification object:nil];
}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
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, activeField.frame.origin) ) {
    [self.scrollView scrollRectToVisible:activeField.frame animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}

似乎uiwebview中的某些点击区域并不总是随着键盘而移动。解决这个问题的一个肮脏的技巧是在按钮/等上放置一个不可见的文本div,并带有一些空格,并让它调用一个javascript函数,该函数试图完成任何未注册的触摸事件。这样:

<div id="someBtn" onclick="tryAction();">&nbsp;&nbsp;&nbsp;&nbsp;</div>

我希望这能有所帮助,或者至少为您指明一个有用的方向。