Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 752
RexOnRoids Avatar asked Mar 22 '10 11:03

RexOnRoids


2 Answers

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.

like image 57
zonble Avatar answered Oct 13 '22 00:10

zonble


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;">
like image 28
cornellouis Avatar answered Oct 12 '22 23:10

cornellouis