wkwebview - 如何使用 wkwebview 调整网页视图
问题描述
我正在加载一个到 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
导入 Foundation 导入 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)
}}
如照片和视频所示,我不知道如何阻止它移动,所以它不显示作为网页一部分的蓝色边。
解决方案
如果要阻止页面滚动到一侧,则显示的 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)
}
希望这会有所帮助。
推荐阅读
- python - Django Translation - 运行服务器时复数形式的无效令牌
- csv - 使用 Windows 行结尾修复损坏的 CSV
- python - pdb 在 pdb 模式下进入函数
- git - 如何通过参数传递给詹金斯工作?
- amazon-web-services - 通过 Filezilla 连接到 AWS Transfer(SFTP 服务)时出错
- python - Python - 如何按照指定的属性顺序对对象数组进行双重排序?
- git - Jenkins 实例丢失
- string - 如何将对象系列转换为字符串系列?
- c++ - 模板类作为 Stl 容器参数
- c# - 查找给定单词的最常见字符