UIWebView - white background before loading conten

2020-07-06 08:45发布

问题:

I have a question. I have a menu, when I click on it, it will load content remotely from URL via UIWebView.

When I try on simulator, before loading the content, there's a white background and then loads my HTML Page (which has dark background)

Demo:

You can see that white flash, when I click on "KALENDER"

How can I fix this issue?

My UIWebView looks like this:

        let myWebView:UIWebView = UIWebView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
        myWebView.loadRequest(NSURLRequest(URL: NSURL(string: "http://website.com/test.html")!))
        self.view.addSubview(myWebView)

Thank you!

回答1:

So, what I did, is, added:

myWebView.opaque = false
myWebView.backgroundColor = UIColor.clearColor()

and at controller, I unchecked this from "View"



回答2:

You cannot set background color to your webView.

You should try following steps:

1. write following in your viewDidLoad - Setting delegate and hiding webView.

webView.delegate = self

webView.hidden = YES

2. Write delegate - shouldStartLoadWithRequest - hiding webView when loading starts.

func webView(webView: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool {
webView.hidden = YES
return true;
}

3. Write delegate - webViewDidFinishLoad - Show webView when loading Ends.

func webViewDidFinishLoad(webView: UIWebView!) {
webView.hidden = NO
print("Webview did finish load")
}

Hope this will help you....



回答3:

You could load your own html document, which uses the color you need.

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    let htmlString = "<html><body bgcolor=\"#ABCDEF\"></body></html>"
    webView.loadHTMLString(htmlString, baseURL: nil)
}