試試我的代碼將幫助你!
第1步:將UIWebView
和UIActivityIndicatorView
提取到您的視圖控制器。 步驟2:委託UIWebView
查看控制器併爲UIWebView
和UIActivityIndicatorView
創建插座。
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.
}
}
如果你混淆或使用本地HTML文件,然後看到這個Youtube tutorial
你爲什麼要設置'alpha'爲0? – eMKa
只是爲了測試它。我想也許它解決了這個問題。 –
所以,即使沒有'alpha'您的'webView'閃爍?什麼意思閃爍? – eMKa