html - 加载页面时 UIWebView 白色闪烁

标签 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 步:获取 UIWebViewUIActivityIndi​​catorView 到您的 View Controller 。 第 2 步:将 UIWebView 委托(delegate)给 viewController 并为 UIWebViewUIActivityIndi​​catorView 创建导出。

第 3 步:在 View Controller 上选择 UIActivityIndi​​catorView,然后转到 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.
    }

}

enter image description here

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

关于html - 加载页面时 UIWebView 白色闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38745264/

相关文章:

javascript - 使用jquery定位元素的top属性

ios - UITableView 中的数据在特定情况下不会刷新

ios - 为 Alamofire 请求输入用户名和密码

Swift:是否可以完全操作TextField、Button等网页元素?

iphone - 为什么我的 nsurl 请求没有出现在控制台中?

iphone - 如何通过 iOS (iPhone) 代码在网页内按下带有 java 脚本事件的按钮?

html - 如何根据父元素选择器条件设置多个子元素的样式

javascript - 在现有 div 中添加带有 Id 的新 div

jquery - 窗口大小为 480px 后显示 'more' 选项

ios - 访问 UICollectionView 的父 UIViewController