【问题标题】:Auto scroll bottom of page uiwebview自动滚动页面uiwebview底部
【发布时间】:2010-12-06 11:09:43
【问题描述】:

我希望一旦 webview 加载

- (void)webViewDidFinishLoad:(UIWebView *)webBrowser {
    //Scroll to bottom
}

只关注底部,所以基本上只显示到 uiwebview 的底部并可能将其锁定在那里,因此用户无法滚动。

【问题讨论】:

    标签: ios objective-c uiwebview


    【解决方案1】:

    首先你必须得到内容高度,我从来没有亲自做过,但here 是一个很好的起点:

    这里有一些可用的常量 给出窗口的文档区域 可用于写入。 这些将不可用,直到 加载文档后, 用于引用它们的方法是 浏览器特定。可用的 常量是:

    window.innerHeight/Width 提供者 大多数浏览器,但重要的是,不是 IE浏览器。 document.body.clientHeight/Width 由许多浏览器提供,包括 IE浏览器。 文档.文档元素。 clientHeight/Width 大多数提供 DOM 浏览器,包括 Internet 探险家。

    然后滚动到你想要的偏移量是这样的:

    [webView stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat:@"scrollTo(0,%d)",scrollPosition]; 
    

    【讨论】:

      【解决方案2】:

      webview 内部的操作由 JavaScript 控制,因此您可以在 Scroll Automatically to the Bottom of the Page 找到各种解决方案。

      基于 Tho 答案 (var scrollingElement = (document.scrollingElement || document.body); scrollingElement.scrollTop = scrollingElement.scrollHeight;) 的示例:

      使用 UIWebView(已弃用)

      DispatchQueue.global().async {
          _ = webView.stringByEvaluatingJavaScript(from: "var scrollingElement = (document.scrollingElement || document.body); scrollingElement.scrollTop = scrollingElement.scrollHeight;")
      }
      

      使用 WKWebView (iOS 8+)

      webView.evaluateJavaScript("var scrollingElement = (document.scrollingElement || document.body); scrollingElement.scrollTop = scrollingElement.scrollHeight;")
      

      【讨论】:

        猜你喜欢
        • 2012-07-27
        • 1970-01-01
        • 1970-01-01
        • 2011-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多