手势后退/前进时WkWebview闪烁白色

问题描述 投票:0回答:1

我正在使用 WkWebview、Vue.js 制作一个 iOS 应用程序。 网站的主题始终是深色模式,因此每个背景颜色都是深色

#181818

当手势后退/前进时,屏幕会显示前一页/下一页的预览,对吧? 有时它什么也看不出来。 (我觉得和缓存或者SPA有关)

没关系。它终于很好地加载了页面。

但是在WkWebview中,后退/前进时背景会闪烁白色。 webview screenshot

我以为html的body颜色是白色,其实也是

#181818

safari screenshot

在野生动物园中,它显示

#181818
很好..

我该如何修复它?

ViewController.swift:

import UIKit
import WebKit

func hexStringToUIColor (hex:String) -> UIColor {
    var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
    
    if (cString.hasPrefix("#")) {
        cString.remove(at: cString.startIndex)
    }
    
    if ((cString.count) != 6) {
        return UIColor.gray
    }
    
    var rgbValue:UInt64 = 0
    Scanner(string: cString).scanHexInt64(&rgbValue)
    
    return UIColor(
        red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
        green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
        blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
        alpha: CGFloat(1.0)
    )
}

class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler {
    
    @IBOutlet weak var containerView: UIView!
    var webView: WKWebView!
    
    
    var baseURL = "http://example.com"
    
    override func loadView() {
        super.loadView()
        
        containerView.backgroundColor = hexStringToUIColor(hex: "#121212")
        containerView.isOpaque = false
        
        let contentController = WKUserContentController()
        let config = WKWebViewConfiguration()
        
   
        config.userContentController = contentController
        config.preferences.javaScriptCanOpenWindowsAutomatically = true
        
        webView = WKWebView(frame: self.containerView.frame, configuration: config)
        webView.uiDelegate = self
        webView.navigationDelegate = self
        webView.isOpaque = false
        webView.isHidden = false
        webView.allowsBackForwardNavigationGestures = true
        webView.scrollView.contentInsetAdjustmentBehavior = .never
        webView.backgroundColor = UIColor.clear
        webView.scrollView.backgroundColor = hexStringToUIColor(hex: "#121212")
        webView.allowsBackForwardNavigationGestures = true
        
        self.view.addSubview(webView)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.isOpaque = false
        self.containerView.isOpaque = false
        webView.isOpaque = false
        
        let backgroundColor = hexStringToUIColor(hex: "#121212")
        webView.backgroundColor = backgroundColor
        super.viewDidLoad()
        
        let url = URL(string:baseURL)
        openApp(url: url!)
//        webView.addObserver(self, forKeyPath: "URL", options: .new, context: nil)
    }
    
    func openApp(url: URL){
        let request = URLRequest(url: url, cachePolicy: URLRequest.CachePolicy.useProtocolCachePolicy)
        webView.evaluateJavaScript("navigator.userAgent") { [weak webView] (result, error) in
            if let webView = webView, let userAgent = result as? String {
                let currentVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
                
                webView.customUserAgent = userAgent + "ios/" + currentVersion!
            }
        }
        
        webView.load(request)
    }
}
ios swift vue.js webview wkwebview
1个回答
-1
投票

有人解决吗?也许是网页html的错误?有些页面必须发生,但有些页面会发生一点,如果我在canGoBack状态改变时改变webview框架,这种情况每次都会发生...

© www.soinside.com 2019 - 2024. All rights reserved.