加载页面时UIWebView白色闪烁

时间:2016-08-03 13:37:04

标签: html swift uiwebview

我已经为iOS构建了一个WebApp。当我点击/点击移动到页面时,例如从页面 - 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 个答案:

答案 0 :(得分:1)

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

第1步:将UIWebViewUIActivityIndicatorView提取到视图控制器。 第2步:将UIWebView委托给UIWebViewUIActivityIndicatorView的viewController和创建商店。

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

第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()

    }


}

在Swift 3.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.
    }

}

enter image description here

如果您混淆或使用本地html文件,请参阅此Youtube tutorial