如何以编程方式滚动iOS WKWebView,迅速4 [英] How to Programmatically Scroll iOS WKWebView, swift 4

查看:276
本文介绍了如何以编程方式滚动iOS WKWebView,迅速4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So my question is: After a website(any websites on the www) has been loaded up in my iOS webview app how to make the app Programmatically Scroll vertically to any of the contents thats out of view?

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

  }
}

推荐答案

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

This will also account for the possible horizontal offset of the origin of the 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)

这篇关于如何以编程方式滚动iOS WKWebView,迅速4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆