UIWebView background is set to Clear Color, but it is not transparent

IphoneObjective CIosCocoa TouchUiwebview

Iphone Problem Overview


I'm developing an iOS 4 application using iOS SDK latest version and XCode 4.2.

I have a XIB with a UIWebView with Alpha = 1.0, Background set to Clear Color and Opaque is not set. On this XIB I setup an image as background with this code:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"AboutBackground.png"]];
        self.view.backgroundColor = background;
        [background release];
    }
    return self;
}

The UIWebView is showing an static html:

<html><head></head><body style=\"margin:0 auto;text-align:center;background-color: transparent; color:white\">...</body></html>

On iOS 5 simulator, its background is transparent, but on an iOS 4 device is grey.

Any clue?

Iphone Solutions


Solution 1 - Iphone

Also set :

[webView setBackgroundColor:[UIColor clearColor]];
[webView setOpaque:NO];

Solution 2 - Iphone

     /*for ios please set this*/

     [webViewFirst setOpaque:NO];
     
     /*for html please set this*/
     <body style="background:none">

Solution 3 - Iphone

Besides setting your webview's background to clear color, also make sure that you set opaque to false.

Solution 4 - Iphone

You can try this code (i know, that it's unsafe, but it works even for ios5):

- (void)makeBodyBackgroundTransparent {
for (UIView *subview in [webView subviews]) {
[subview setOpaque:NO];
[subview setBackgroundColor:[UIColor clearColor]];
}
[webView setOpaque:NO];
[webView setBackgroundColor:[UIColor clearColor]];
}

Solution 5 - Iphone

webView.opaque = NO;

webView.backgroundColor = [UIColor clearColor];

Solution 6 - Iphone

NSString *content=@"example clear background color UIWebview";
NSString *style=[NSString stringwithformat:@"<html><head><style>body {background-color:transparent;}</style></head><body>%@</body></html>",content];
[myWebview loadhtmlstring:style baseurl:nil];

Solution 7 - Iphone

Latest Swift Version (as required):

lazy var webView: UIWebView = {
    let view = UIWebView()
    view.delegate = self
    view.backgroundColor = .clear
    view.isOpaque = false
    return view
}()

Remove delegate line if not required

Solution 8 - Iphone

For Objective

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

Please make sure to include this into your HTML code:

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

or

 <body style="background:none">

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
QuestionVansFannelView Question on Stackoverflow
Solution 1 - IphoneMaulikView Answer on Stackoverflow
Solution 2 - IphonedheerendraView Answer on Stackoverflow
Solution 3 - IphoneElegiaView Answer on Stackoverflow
Solution 4 - IphoneArtem StepanenkoView Answer on Stackoverflow
Solution 5 - IphoneAvinash651View Answer on Stackoverflow
Solution 6 - Iphoneuser5742417View Answer on Stackoverflow
Solution 7 - IphoneRichAppzView Answer on Stackoverflow
Solution 8 - IphoneAmr AngryView Answer on Stackoverflow