UIWebView background color

IosUiwebview

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

Use this an addition to your code

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

Solution 3 - Ios

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

Solution 4 - Ios

Tested in Xcode8 and Swift 3

self.webView.isOpaque = false;
self.webView.backgroundColor = UIColor.clear

Solution 5 - Ios

UIWebView *webview =[[UIWebView alloc];

[webview setBackgroundColor:[UIColor whiteColor]];

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNamhcirView Question on Stackoverflow
Solution 1 - IosKalView Answer on Stackoverflow
Solution 2 - IosashokdyView Answer on Stackoverflow
Solution 3 - IosNilesh PatelView Answer on Stackoverflow
Solution 4 - IosMuseer Ahamad AnsariView Answer on Stackoverflow
Solution 5 - Iosharish babuView Answer on Stackoverflow