Prevent UIWebView white flashing issue

Recently have this problem in my project. Take me a while to resolve the issue. I was loading UIWebView and execute some Javascript to dynamically update the local HTML.

Send command to webview

NSMutableDictionary* jsonDit = ***; // create a dictionary here 

// use SBJSON to convert dictionary to a JSON. use this to escape the string

// this json will be executed by the page

NSString *js = [[NSString alloc]initWithFormat:@"setHtml(%@);", [jsonDit JSONRepresentation]  ];

[self.webview stringByEvaluatingJavaScriptFromString:js];

Fix white flashing issue by using core animation

Add this to viewDidLoad

// 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

- (void)webViewDidFinishLoad:(UIWebView *)webView
 // flicker fix

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:0.30];

    self.webview.alpha = 1;

    [UIView commitAnimations];

comments powered by Disqus