如何以编程方式滚动iOS WKWebView,迅速4

时间:2018-08-02 17:27:33

标签: swift animation wkwebview cgpoint contentoffset

所以我的问题是:在我的iOS webview应用程序中加载了一个网站(www上的任何网站)后,如何使该应用程序以编程方式垂直滚动到看不见的任何内容?

//  ViewController.swift
//  myWebView

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {

  @IBOutlet weak var myWebView: WKWebView!

  override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.
  }

  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear( animated )

    let urlString:String = "https://www.apple.com"
    let url:URL = URL(string: urlString)!
    let urlRequest:URLRequest = URLRequest(url: url)

    myWebView.load(urlRequest)
  }


  func webViewDidFinishLoad(_ webView: WKWebView) {

    let scrollPoint = CGPoint(x: 0, y: webView.scrollView.contentSize.height - webView.frame.size.height)
    webView.scrollView.setContentOffset(scrollPoint, animated: true )

  }
}

3 个答案:

答案 0 :(得分:1)

您可以操纵网络视图滚动视图。下面有一个滚动到视图底部的示例:

let scrollPoint = CGPoint(x: 0, y: yourWebView.scrollView.contentSize.height - webView.frame.size.height)
webView.scrollView.setContentOffset(scrollPoint, animated: true)

答案 1 :(得分:0)

这还将考虑scrollView的原点的可能水平偏移:

var scrollPoint = self.view.convert(CGPoint(x: 0, y: 0), to: webView.scrollView)
scrollPoint = CGPoint(x: scrollPoint.x, y: webView.scrollView.contentSize.height - webView.frame.size.height)
webView.scrollView.setContentOffset(scrollPoint, animated: true)

答案 2 :(得分:0)

如果您想使用此扩展名来通知您的wbeview到达顶部或底部,我很久以前就使用wkwebview。 webview reach top or bottom