【问题标题】:UIWebView scrolling when keyboard appears. Causing tap offset bug?出现键盘时 UIWebView 滚动。导致抽头偏移错误?
【发布时间】:2013-10-28 21:07:29
【问题描述】:

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

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

基本上是这样,所以我点击顶部输入字段,并且点击注册的位置比它应该低约 20 像素,因为视图正在被键盘移动。

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

感谢任何帮助!

【问题讨论】:

    标签: javascript ios iphone uiwebview


    【解决方案1】:

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

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

    不确定您是否找到此页面,但您可以添加键盘通知侦听器并自行操作滚动。这是 Apple 的文档链接: 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>
    

    我希望这有助于或至少为您指明一个有用的方向。

    【讨论】:

    • 你从哪里得到变量 activeField?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 2015-10-28
    • 2017-08-09
    • 2014-02-03
    • 2019-04-01
    • 2013-05-21
    相关资源
    最近更新 更多