作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个独立的WKWebViewController
。当按下按钮时,我先加载WKWebView
,然后加载另一个WKWebView
。
所以我想提前加载另一个WKWebView
。我怎样才能做到这一点?
两个ViewController均具有WKWebView。
第一个WKWebView
func openSecondScreen(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let openSecondScreen = storyboard.instantiateViewController(withIdentifier: "SecondWebViewController") as! SecondWebViewController
openSecondScreen.modalPresentationStyle = .overCurrentContext
openSecondScreen.modalTransitionStyle = .crossDissolve
openSecondScreen.delegate = self
self.navigationController?.pushViewController(openSecondScreen, animated: true)
}
@IBOutlet var secondWKWebView: WKWebView!
override func loadView() {
super.loadView()
let images = global.getIndicatorImage()
let animatedImage = UIImage.animatedImage(with: images, duration: 1.0)
self.indicatorImage.image = animatedImage
contentController.add(self, name: "SecondWeb")
config.userContentController = contentController
secondWKWebView = WKWebView(frame: secondWKWebView.frame, configuration: config)
secondWKWebView.uiDelegate = self
secondWKWebView.navigationDelegate = self
view.addSubview(secondWKWebView)
view.addSubview(indicator)
if contentController.userScripts.count > 0 {
contentController.removeAllUserScripts()
}
let localFilePath = Bundle.main.url(forResource: webUrl, withExtension: "html")
let myRequest = URLRequest(url: localFilePath)
secondWKWebView.load(myRequest)
}
最佳答案
我想我找到了解决方案。但是有一个问题。我仍然可以看到指示器的动画图像。我认为搜索不完整。
第一个WKWebView
var openSecondScreen : SecondWebViewController!
var preloadCheck = false
...
func openSecondScreen(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
openSecondScreen = storyboard.instantiateViewController(withIdentifier: "SecondWebViewController") as! SecondWebViewController
openSecondScreen.delegate = self
openSecondScreen.loadViewIfNeeded()
openSecondScreen.secondWKWebView.uiDelegate = self
openSecondScreen.secondWKWebView.navigationDelegate = self
preloadCheck = true
}
....
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if preloadCheck {
self.navigationController?.pushViewController(openSecondScreen, animated: true)
preloadCheck = false
}
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
indicatorImage.isHidden = true
indicatorImage.stopAnimating()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
indicatorImage.isHidden = true
indicatorImage.stopAnimating()
}
secondWebview
屏幕时,
firstWebview
屏幕上显示
指示器图像,导航完成后
指示器图像消失,并且
secondWebview
屏幕出现
preloaded
。
关于ios - 如何在swift5中从WKWebviews预加载WKWebviews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58796829/
我是一名优秀的程序员,十分优秀!