【问题标题】:How to disabled uiwebview auto scroll to top when finished load加载完成后如何禁用uiwebview自动滚动到顶部
【发布时间】:2014-11-19 03:39:17
【问题描述】:

我在 UIWebView 的滚动视图中添加了相同的子视图。当 webview 获取 contentSize 时,滚动视图将滚动到顶部。现在我知道事件在完成之前发生(某些图像可能仍在加载)但在获取文本内容之后。我应该怎么做怎么办?

这是代码,要查看您需要减慢请求的错误(使用边缘)!在加载完成之前滚动视图,然后等待。当请求完成时,您将看到视图滚动到顶部。

class WebViewController: UIViewController {
var mw = UIScreen.mainScreen().bounds.width
var mh = UIScreen.mainScreen().bounds.height

override func viewDidLoad() {
    super.viewDidLoad()

    var web = UIWebView(frame: CGRect(x: 0, y: 0, width: mw, height: mh))

    var scrollview = web.scrollView

    var tmp = UIView(frame: CGRect(x: 0, y: 0, width: mw, height: 300))

    tmp.backgroundColor = UIColor.redColor()

    scrollview.addSubview(tmp)

    (scrollview.subviews.first as UIView).frame.origin.y = 300

    let requestURL = NSURL(string: "http://stackoverflow.com/questions/27008545/how-to-disabled-uiwebview-auto-scroll-to-top-when-finished-load")

    let request = NSURLRequest(URL: requestURL!)

    web.loadRequest(request)

    self.view.addSubview(web)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

【问题讨论】:

    标签: ios swift uiwebview webviewdidfinishload


    【解决方案1】:

    首先您需要将 setContentSize: 设置为实际的内容大小,而不是框架大小。也就是说,如果您的内容是例如800px 高,你应该设置ContentSize:CGSizeMake(width, 800)。使用它,您可以设置一个 UIScrollView,它应该滚动以显示 800px 高的内容。如果您将 UIScrollView 的内容大小设置为等于其框架,则不会执行滚动,也不会出现滚动指示器。

    【讨论】:

      猜你喜欢
      • 2012-01-08
      • 1970-01-01
      • 1970-01-01
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 2012-04-29
      • 2012-04-04
      • 1970-01-01
      相关资源
      最近更新 更多