2016-08-03 111 views
1

我已經構建了適用於iOS的WebApp。當我點擊/點擊移動到頁面 - 例如從頁面 - index.html到page2.html,它閃爍白色。你知道爲什麼,以及如何解決這個問題?UIWebView在加載頁面時閃爍白色

網站在瀏覽器(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. 
} 


} 
+0

你爲什麼要設置'alpha'爲0? – eMKa

+0

只是爲了測試它。我想也許它解決了這個問題。 –

+0

所以,即使沒有'alpha'您的'webView'閃爍?什麼意思閃爍? – eMKa

回答

1

試試我的代碼將幫助你!

第1步:將UIWebViewUIActivityIndicatorView提取到您的視圖控制器。 步驟2:委託UIWebView查看控制器併爲UIWebViewUIActivityIndicatorView創建插座。

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

    } 


} 

在雨燕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

+0

感謝您的代碼。它的工作原理,但對我的網站來說,加載需要很長時間。我的網站是一個單頁網站,其中有很多部分/面板非常大。高度是38400px(768px * 50)。鏈接是錨鏈接。 ** 2016-08-04 11:36:38.952 webApp [3058:3565120] App Transport Security已阻止明文HTTP(http://)資源加載,因爲它是不安全的。臨時例外可以通過您的應用程序的Info.plist文件進行配置。 2016-08-04 11:36:41.970 webApp [3058:3565039]收到內存警告。 2016-08-04 11:36:42.166 webApp [3058:3565039]接收到內存警告。** –

+0

請問,Check Youtube教程:)我在視頻中顯示了你需要的東西。 https://youtu.be/6iHt6UEZQ88) –

+1

非常感謝你的努力!我觀看了視頻並在Xcode項目中添加了代碼,但閃爍/閃爍問題仍然存在:( –

相關問題