我已经为 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 步:获取 UIWebView
和 UIActivityIndicatorView
到您的 View Controller 。
第 2 步:将 UIWebView
委托(delegate)给 viewController 并为 UIWebView
和 UIActivityIndicatorView
创建导出。
第 3 步:在 View Controller 上选择 UIActivityIndicatorView
,然后转到 Attributes Inspector ->Activity Indicator View -> 检查 Animating and Hides when Stopped on Behaviour
第 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.
}
}
如果您对使用本地 html 文件感到困惑,请参阅此 Youtube tutorial
关于html - 加载页面时 UIWebView 白色闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38745264/