【问题标题】:How to adjust webpage view using wkwebview如何使用 wkwebview 调整网页视图
【发布时间】:2019-11-17 00:08:08
【问题描述】:

我正在加载一个指向 wkwebview 的 url,我想阻止它像这样查看网页的一侧

https://drive.google.com/file/d/1qStJ1C_rrcjobSuorgPeLTkHZc82oUV4/view?usp=sharing

而不是

https://drive.google.com/file/d/1G05f6UVolmQCqdqKdJ0UFBBA6LVDI-BN/view?usp=sharing

Scrollview 已经是假的,并且反弹也是假的。谁能帮帮我

谢谢!

我已经尝试使用滚动视图和反弹参数,但 wkwebview 仍然向左移动,露出图像中显示的蓝色部分。我想显示第一张图片中显示的网页,以便它的视图被锁定,因为没有更好的词。我不确定我是否解释得很好,但如果我需要解释,请告诉我。我在下面添加了一个视频,显示我想知道如何做。

https://drive.google.com/file/d/1uErGQlB1HKLU7px49UtGbsX9nOYqa3FN/view?usp=sharing

导入基础 导入 UIKit 导入 WebKit 类 ViewControllerWebKit: UIViewController, WKUIDelegate {

var webView: WKWebView!

override func loadView() {


    let webConfiguration = WKWebViewConfiguration()
    webView = WKWebView(frame: .zero, configuration: webConfiguration)
    webView.uiDelegate = self
    view = webView
}
override func viewDidLoad() {
    super.viewDidLoad()
     webView.scrollView.bounces = false
    let myURL = URL(string:"https://www.swpc.noaa.gov/communities/space-weather-enthusiasts")


   let myRequest = URLRequest(url: myURL!)
    webView.load(myRequest)
}}

如照片和视频所示,我不知道如何让它不动,所以它没有显示网页的蓝色部分。

【问题讨论】:

  • 这里是图片,因为它没有显示:

标签: wkwebview swift5


【解决方案1】:

如果你想阻止页面滚动到一边,你显示的 html 中的宽度应该等于 device-width。您可以通过定义viewport 来做到这一点。问题是您的页面右侧有蓝色条,因此无论哪种方式,我认为您都必须显示它。 此解决方案将停止滚动行为,但该栏仍会显示,因为它是页面的一部分。

override func viewDidLoad() {
    super.viewDidLoad()

    let jscript = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width');  document.getElementsByTagName('head')[0].appendChild(meta);"
    let userScript = WKUserScript(source: jscript, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
    let wkUController = WKUserContentController()
    wkUController.addUserScript(userScript)
    let webConfiguration = WKWebViewConfiguration()
    webConfiguration.userContentController = wkUController


    webView = WKWebView(frame: .zero, configuration: webConfiguration)
    webView.uiDelegate = self
    view = webView

    webView.scrollView.bounces = false
    let myURL = URL(string:"https://www.swpc.noaa.gov/communities/space-weather-enthusiasts")

    let myRequest = URLRequest(url: myURL!)
    webView.load(myRequest)
}

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-26
    • 2015-01-04
    • 2018-01-25
    • 2013-04-22
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    相关资源
    最近更新 更多