How to control the color of a UIWebView (before contents load)?

18,287

Solution 1

Actually setting the background color cannot make the web view entire black. The content of a web page is presented in a subview within the web view, a web view contains many undocumented subviews, including a UIScrollView which allows you to scroll the contents, and a UIScrollView contains UIWebDocumentView, which draws the web contents. They are above the background of your web view, so they cover the background, what you can see is the UIWebDocumentView and setting the background color cannot make your web view entire black.

I guess the solution is, you may try to place a black view over your web view, and hide the view when the delegate methods such as webViewDidFinishLoad: or webViewDidStartLoad: are called.

Solution 2

The way I did this was to put the UIWebView on a black colored view. Then, we set the web page and the webview to be clear. Works in iOS 4.2 .

On the superview:

view.backgroundColor = [UIColor blackColor];

On the webView:

webView.opaque = NO;
webView.backgroundColor = [UIColor clearColor];

In the Web HTML:

<body style="background-color:transparent;">

Solution 3

Actually, this is really simple:

[self.webView setOpaque:NO];
self.webView.backgroundColor = [UIColor purpleColor];

This will show the background color, and as soon as the webview loads it will, happily, disappear.

Solution 4

to remove this white flicker I only change backgroundColor to the color of my view. my code uses purple to show more general example. black is used simply as UIColor.black

editing loadView is not working for me, but viewWillAppear worked fine

override func viewWillAppear(_ animated: Bool) {
    ...
    webView.isOpaque = false
    webView.backgroundColor = UIColor(red: 41/255, green: 45/255, blue: 91/255, alpha: 1)
    ...
}

Solution 5

Try This it work for me,

Follow below steps ,

Set UIview and webview opaque = NO and background Color is white

enter image description here

Add below line of code before load of web view,

  _webViewOutlet.backgroundColor = [UIColor colorWithWhite:1 alpha:1]; 

Hope so this is help for someone

Share:
18,287
RexOnRoids
Author by

RexOnRoids

Just some kid.

Updated on June 05, 2022

Comments

  • RexOnRoids
    RexOnRoids almost 2 years

    I am trying to have a BLACK colored UIWebView before its contents load. In IB, i tried both making the background color of the UIWebView black, as well as, making it have a transparent background and have its parent view's background be black. Both don't work. When my UIWebView loads the background of it is white. How can I fix this?

    *I hope this does not boil down to having to load an html string first (before loading my actual web content) to set the background to black vis CSS.

  • alexleutgoeb
    alexleutgoeb over 13 years
    The less memory prone solution would be to hide/show the webview itself on viewwillappear or didstartload/didfinishload (that's actually the way i do it).
  • Mona
    Mona over 11 years
    webView.opaque = NO; webView.backgroundColor = [UIColor clearColor]; worked for me! thanks
  • Jonathan
    Jonathan over 10 years
    I can confirm this is still working on iOS 6.1 and a certain newer version of iOS :)
  • Kassem
    Kassem almost 10 years
    [webView setOpaque:NO];