开发者

Preloading a UIWebView, avoiding white screen flash

开发者 https://www.devze.com 2023-03-05 08:11 出处:网络
I\'m working on an app that has table navigation to eventually drill down to UIWebView that displays various information. However, the transition from the slickness of UITableView to the slow wonkines

I'm working on an app that has table navigation to eventually drill down to UIWebView that displays various information. However, the transition from the slickness of UITableView to the slow wonkiness of UIWebView is jarring for the user so I want to improve that experience however I can.

Specifically, the background of both the tableViews and UIWebView pages have black backgroun开发者_高级运维ds, but when I open the UIWebView it flashes empty white for about a second (this is the case for both local and remote HTML files.) How can I (ideally) preload this process, or (at least) make the "flash" be all black rather than all white? I tried making the view / webView's background black but that didn't seem to help.

In my app right now, when a user selects a cell, the app just loads up the UIWebView subclass and pushes it on the navigation stack. The UIWebView subclass has an activity indicator that starts & stops animating on WebViewDidStartLoad and WebViewDidFinishLoad, which works fine, but it doesn't do anything to help the "white flash."


I have tested it... I'm sending the two method that I have used...

 - (void)viewDidLoad {
        [super viewDidLoad]; //objWebView is the outlet of UIWebView
        [objWebView loadHTMLString:@"<html><body style=\"background-color:black;\"></body></html>" baseURL:nil];

        //the more the delay the errors will be less so within 0.1-0.3 would be fine
        [self performSelector:@selector(loadURL:) withObject:nil afterDelay:0.1]; 
    }

-(void)loadURL:(id)sender{
    [objWebView stopLoading]; //added this line to stop the previous request
    NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    [objWebView loadRequest:req];
}

here I'm performing the request after 0.1 sec, other wise it will look white as in your case. Or you can give your delay time depending upon the time.. Cheers :)


Try in your

    -(void)viewDidLoad{
       myWebView.hidden = YES;

Then in

    -(void)loadURL:(id)sender{  
        myWebView.hidden = NO;


I used:

[webView setHidden:YES];  
[webView setDelegate:self];

when creating my webView and making the request and then added this delegate method to handle completed requests:

- (void) webViewDidFinishLoad:(UIWebView*)webView{  
    [webView setHidden:NO];  
}


Successfully done it. You have to create a view in Interface Builder first. Then load the html to the webview using a initWithFrame in the init of your ViewController that contains the webview(this is where the magic happens):

CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
webView = [[UIWebView alloc] initWithFrame:webFrame];

Then simply load the webView into the view in viewWillAppear:

[viewWeb addSubview:webView];

This is really a question of interface designing, which is faster paint directly on the view or paint in a subview and then paint that subview in the view?


I had solved this problem years ago, using the common method of hiding the UIWebView behind a UIImageView, then removing the UIImageView after a delay.

But it suddenly came back, I think on iOS 7.0.4. It was occurring on a brand new iPad Air, as well as an older iPad mini non-retina. After two days of hair-pulling, I finally found a work-around.

Let's say you have webview which is restricted to landscape orientation, initialized like this:

WebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024.0f, 768.0f)];

And then you make it visible after pre-loading, with eg bringSubviewToFront or setHidden:NO on the webview (or alternatively with setHidden:YES or removeFromSuperview on the UIImageView). But instead of seamlessly switching the views, there's a flash and the background color blinks for about half a second.

The fix is to change the size of your webview, ever so slightly:

WebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024.01f, 768.0f)];

The problem and fix is highly reproducible. It works with a change as slight as the fourth decimal place (1024.0001f). At the fifth decimal place (1024.00001f), the flash returns. The value of the height (768.0f) didn't seem to matter.


Or, instead of working around the problem, you could just set the background color of your webview to whatever background color you're using. Unless you're using an image of course.

0

精彩评论

暂无评论...
验证码 换一张
取 消