64

I am loading an HTML string into a UIWebView in order to be able to view rich text. So far, so good. But I have one small problem: in my Nib file, I set the background attribute to green. However, when it is displayed, the background is white. Then, in the class file, I added the following

[myWebView setBackgroundColor:[UIColor greenColor]];
    [myWebView loadHTMLString:myString baseURL:nil];

However, the background is still white. I even tried reversing the order, but still comes out white background.

There is more text than the size of the UIWebView. I noticed that when I scroll down past the end of the text, the background is then green. How do I get the background for the text itself to be green?

Could you please advise me as to what I am doing wrong? Muchly appreciated.

Cezar
  • 55,636
  • 19
  • 86
  • 87
Namhcir
  • 825
  • 3
  • 9
  • 16

5 Answers5

160

Set the background color in your html itself using css markup.

Or, set the webview's opaque property to NO .. and set the background of the view underneath to green.

UIWebView themselves don't seem to understand background color-- since they are rendering documents.

Nilesh
  • 701
  • 5
  • 14
Kal
  • 24,724
  • 7
  • 65
  • 65
  • 6
    Yes, thank you1 I added setting opaque to NO, and it's perfect now. – Namhcir Jul 07 '11 at 02:44
  • But one more question--I also need to do display rich view in the tableview cells of a tableview in a prior view. Will I get into trouble if I add a webview to a tableviewcell? Otherwise, how do I display rich html in a tableview? – Namhcir Jul 07 '11 at 02:46
  • Since they are all uiviews you could get away with adding it. But you should review some of Apple's HIG and the UITableView API reference. There are several ominous warnings there about overriding UITableCellView and making the background transparent. – Kal Jul 07 '11 at 02:50
  • Adding UIWebViews to UITableViewCells can have serious performance implications. You will definitely want to test your implementation. Or you could look at how some open source libraries have implemented rich text table view cells, Three20 is one example. – Eric Jan 11 '12 at 22:23
  • [aWebView setOpaque:NO]; solved the problem. A MILLION THANKS – DD_ Jan 24 '13 at 06:47
  • @Kal What do you think would be better for performance ? Setting it in CSS (and communicating with JavaScript to change it later), or setting it to NOT opaque (which could be have bad implementation in my opinion) ? – Paweł Brewczynski May 08 '14 at 03:30
  • I'm a bit late to the party, but embedding one scrollable view in another without custom scrolling controllers is asking for trouble – Przemysław Wrzesiński May 23 '16 at 19:15
  • webView.opaque = false and setting the back ground color in storyboard worked for me – Rajath Kornaya Aug 13 '20 at 07:36
30

Use this an addition to your code

[myWebView setBackgroundColor:[UIColor greenColor]];
[myWebView setOpaque:NO];
[myWebView loadHTMLString:myString baseURL:nil];
ashokdy
  • 1,001
  • 12
  • 21
  • 1
    The last line did the trick but instead of myString I wrote nil. Then I loaded my actual page. Thanks! – Segev Nov 10 '13 at 08:52
13
webView.opaque = NO;
webView.backgroundColor = [UIColor clearColor];
Nilesh Patel
  • 6,318
  • 1
  • 26
  • 40
12

Tested in Xcode8 and Swift 3

self.webView.isOpaque = false;
self.webView.backgroundColor = UIColor.clear
Museer Ahamad Ansari
  • 5,414
  • 3
  • 39
  • 45
0
UIWebView *webview =[[UIWebView alloc];

[webview setBackgroundColor:[UIColor whiteColor]];
Dharman
  • 30,962
  • 25
  • 85
  • 135