xcode UIWebView - 加载内容前的白色背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29009132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:49:13  来源:igfitidea点击:

UIWebView - white background before loading content

iosxcodeswiftuiwebview

提问by

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

我有个问题。我有一个菜单,当我点击它时,它将通过 UIWebView 从 URL 远程加载内容。

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

当我在模拟器上尝试时,在加载内容之前,有一个白色背景,然后加载我的 HTML 页面(具有深色背景)

Demo:

演示:

https://s3.amazonaws.com/quickcast/6134/84576/quickcast.gif

https://s3.amazonaws.com/quickcast/6134/84576/quickcast.gif

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

当我点击“KALENDER”时,你可以看到白色的闪光

How can I fix this issue?

我该如何解决这个问题?

My UIWebView looks like this:

我的 UIWebView 看起来像这样:

        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!

谢谢!

回答by

So, what I did, is, added:

所以,我所做的是,补充说:

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

and at controller, I unchecked this from "View"

在控制器上,我从“查看”中取消选中

enter image description here

在此处输入图片说明

回答by Nirav Gadhiya

You cannot set background color to your webView.

您不能为 webView 设置背景颜色。

You should try following steps:

您应该尝试以下步骤:

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

1. 在您的 viewDidLoad 中写入以下内容 - 设置委托和隐藏 webView。

webView.delegate = self

webView.hidden = YES

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

2.编写delegate——shouldStartLoadWithRequest加载开始时隐藏webView。

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

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

3.写delegate - webViewDidFinishLoad- 加载Ends时显示webView。

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

Hope this will help you....

希望能帮到你....

回答by Matthias Bauch

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

您可以加载自己的 html 文档,该文档使用您需要的颜色。

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