Can't change background for UIWebView in iOS

Is there a way to change background color for UIWebView?

None of the colors set in IB effect UIWebView behavior: before actual content is loaded it shows as up as white (causing a white flash between the moment it is loaded and content is rendered).

Setting background color programmatically does not do anything either.

Here is code:

@interface Web_ViewViewController : UIViewController {

    UIWebView *web;
}

@property (nonatomic, retain) IBOutlet UIWebView *web;
@end

....
-(void)viewDidLoad {
    super viewDidLoad;

    web.backgroundColor = [UIColor blueColor];
    NSURL *clUrl = [NSURL URLWithString:@"http://www.apple.com"];
    NSURLRequest *req = [NSURLRequest requestWithURL:clUrl];

    [web loadRequest:req];
}

You can set the opaque property of the webview to NO and then set background color of the view beneath.

[webView setOpaque:NO];

Give this a try. It will fade the UIWebView in once it has finished loading and you won't see the flash.

@interface Web_ViewViewController : UIViewController <UIWebViewDelegate> {

UIWebView *web;
BOOL firstLoad;
}

@property (nonatomic, retain) IBOutlet UIWebView *web;
@end

...

(void)viewDidLoad {
    [super viewDidLoad];
    firstLoad = YES;
    web.delegate = self;
    web.alpha = 0.0;
    NSURL *clUrl = [NSURL URLWithString:@"http://www.apple.com"];
    NSURLRequest *req = [NSURLRequest requestWithURL:clUrl];
    [web loadRequest:req];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (firstLoad) {
    firstLoad = NO;
    [UIView beginAnimations:@"web" context:nil];
    web.alpha = 1.0;
    [UIView commitAnimations];
    }
}

The animation block in webViewDidFinishLoad will fade the view in once it's loaded, or if you prefer to have it pop on then just remove the UIView calls.


None of the answers here worked for me, they all involved still some sort of flash. I found a working answer here: http://www.iphonedevsdk.com/forum/iphone-sdk-development/4918-uiwebview-render-speeds-white-background.html (I had to adjust the delay to .25 to avoid the flashing). Remember to check the UIWebView's "hidden" flag in InterfaceBuilder.

- (void)webViewDidFinishLoad:(UIWebView *)webView 
{
    [self performSelector:@selector(showAboutWebView) withObject:nil afterDelay:.25];          
}

- (void)showAboutWebView 
{
    self.infoWebView.hidden = NO;
}

Yes, it seems setting the backgroundColor property has no effect on the UIWebView.

I don't have a satisfactory solution, only a workaround that you can consider.

  • First change the background color, by setting an initial empty HTML page
  • Once that has loaded, you load your main URL (e.g. http://www.apple.com)

If you don't wait for the initial HTML page to load, you might not see the initial background while the main URL is loading. So you'll need to use the UIWebViewDelegate's webViewDidFinishLoad: method. You can implement that method in your Web_ViewViewController class, and make your Web_ViewViewController conform to the UIWebViewDelegate protocol. Initially, there is still a momentary flicker of white background until the empth HTML page loads. Not sure how to get rid of that. Below is a sample:

- (void)viewDidLoad
{
    [web loadHTMLString: @"<html><body bgcolor=\"#0000FF\"></body></html>" baseURL: nil];
    web.delegate = self;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    static BOOL loadedMainURLAlready = NO;
    if (!loadedMainURLAlready)
    {
        NSURL *clUrl = [NSURL URLWithString:@"http://apple.com"];
        NSURLRequest *req = [NSURLRequest requestWithURL:clUrl];
        [webView loadRequest:req];
        loadedMainURLAlready = YES;
    }
}