加载页面时 UIWebView 白色闪烁

UIWebView white flashing when load page

我已经为 iOS 构建了一个 WebApp。当我 click/tap 移动到一个页面时,例如从页面 - index.html 到 page2.html 它闪烁白色。您知道为什么会出现这个问题吗?如何解决?

该网站在浏览器(Safari、IE、Firefox 和 Chrome)中运行良好,但不能作为应用程序运行。

我的代码是:

import UIKit

class ViewController: UIViewController {


@IBOutlet weak var webView: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    webView.loadRequest(NSURLRequest(URL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("index", ofType: "html")!)))


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

试试我的代码会对你有帮助!

第 1 步:获取 UIWebViewUIActivityIndicatorView 到您的视图控制器。 第 2 步:将 UIWebView 委派给 viewController 并为 UIWebViewUIActivityIndicatorView 创建出口。

第 3 步:在视图控制器上 Select UIActivityIndicatorView 然后转到 属性检查器 -> Activity 指示器视图 -> 检查行为停止时的动画和隐藏

第 4 步:将以下代码添加到您的 ViewController

import UIKit

class ViewController: UIViewController,UIWebViewDelegate{

    @IBOutlet var loader: UIActivityIndicatorView!
    @IBOutlet var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.automaticallyAdjustsScrollViewInsets = false //to avoid auto scrolling.
        functionOfWebView()

    }

    func functionOfWebView()
    {
        let URL = NSURL(string: "http://www.google.com")
        //let URL = NSBundle.mainBundle().URLForResource("index", withExtension: "html")  //For local html file(index.html) with local file hyperlink(file.html) see on video tutorial
        let request = NSURLRequest(URL: URL!)
        webView.loadRequest(request)
    }

    func webViewDidStartLoad(webView: UIWebView)
    {
        loader.startAnimating()
    }

    func webViewDidFinishLoad(webView: UIWebView)
    {
        loader.stopAnimating()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }


}

在Swift3.0

import UIKit

class ViewController: UIViewController,UIWebViewDelegate{

    @IBOutlet var loader: UIActivityIndicatorView!
    @IBOutlet var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.automaticallyAdjustsScrollViewInsets = false //to avoid auto scrolling.
        functionOfWebView()
    }

    func functionOfWebView()
    {
        let URL = NSURL(string: "http://www.google.com")
        //let URL = Bundle.main.url(forResource: "index", withExtension: "html")  //For local html file(index.html) with local file hyperlink(file.html) see on video tutorial
        let request = NSURLRequest(url: URL! as URL)
        webView.loadRequest(request as URLRequest)
    }

    func webViewDidStartLoad(_ webView: UIWebView)
    {
        loader.startAnimating()
    }

    func webViewDidFinishLoad(_ webView: UIWebView)
    {
        loader.stopAnimating()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

如果您对使用本地 html 文件感到困惑,请参阅此 Youtube tutorial