Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent initial white flash when showing a UIWebView?

I use a UIWebView to show an "About" screen by displaying a bundled HTML file. My app's view hierarchy is: UITabBarController / UIViewController / UIWebView.

The problem is that the HTML page has a dark background, and the very first time the tab is tapped, a white background is visible briefly before the web view is displayed. I tried setting the background color of the UIWebView, but that doesn't solve the problem. The problem occurs whether I load the content in viewDidLoad or viewWillAppear.

like image 531
Dogweather Avatar asked Mar 27 '13 21:03

Dogweather


2 Answers

Swift:

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

Objective-C:

webView.opaque = NO; webView.backgroundColor = [UIColor clearColor]; 
like image 145
LE SANG Avatar answered Oct 16 '22 06:10

LE SANG


// load index.html

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"www"]];
self.webview.delegate = self;

self.webview.alpha = 0; // flicker fix 

[self.webview loadRequest:[NSURLRequest requestWithURL:url]];

Add this to delegate after loading

// flicker fix

- (void)webViewDidFinishLoad:(UIWebView *)webView  
{
    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:0.30];

    self.webview.alpha = 1;

    [UIView commitAnimations];

}
like image 28
PrasathBabu Avatar answered Oct 16 '22 05:10

PrasathBabu