UIWebView - white background before loading content

与世无争的帅哥 提交于 2019-12-09 16:09:31

问题


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


来源:https://stackoverflow.com/questions/29009132/uiwebview-white-background-before-loading-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!