javascript - 出现键盘时 UIWebView 滚动。导致抽头偏移错误?

标签 javascript ios iphone uiwebview

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

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

从本质上讲,这是成功的,所以我点击顶部的输入字段,点击记录比应有的低 20 像素,因为 View 正在被键盘移动。

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

感谢任何帮助!

最佳答案

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

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

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

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

// 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>

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

关于javascript - 出现键盘时 UIWebView 滚动。导致抽头偏移错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19645094/

相关文章:

iphone - Google Chart API 在 iPhone 上突破容器

iphone - 如何使用 libavcodec (ffmpeg) 读取 http 视频流

javascript - 内联网络 worker : Uncaught SyntaxError: Unexpected identifier

javascript - 如何识别网页是在 iframe 中加载还是直接加载到浏览器窗口中?

javascript - 不同参数的axios GET请求被丢弃

html - 在 ionic/cordova for ios 中使用视频作为背景

ios - 尝试在 iPad 上加载 View 时出现异常 : "loaded the nib but the view outlet was not set", 如何添加该 socket ?

javascript - 带有 ArrayToDataTable 的 Google 柱形图工具提示

ios - Facebook 评论插件 - 登录后文本丢失

iphone - 当 tableview 比屏幕长时将其转换为图像