预加载 UIWebView,避免白屏闪烁

发布于 2024-11-07 03:04:41 字数 542 浏览 0 评论 0原文

我正在开发一个具有表格导航的应用程序,最终可以深入到显示各种信息的 UIWebView 。然而,从流畅的 UITableView 到缓慢的 UIWebView 的过渡对用户来说是不舒服的,所以我想尽我所能地改善这种体验。

具体来说,tableViewsUIWebView 页面的背景都是黑色背景,但是当我打开 UIWebView 时,它会闪烁空白色大约一秒钟(本地和远程 HTML 文件都是这种情况。)我如何(理想地)预加载此过程,或者(至少)使“flash”全黑而不是全白?我尝试将视图/webView 的背景设为黑色,但这似乎没有帮助。

现在在我的应用程序中,当用户选择一个单元格时,应用程序仅加载 UIWebView 子类并将其推送到导航堆栈上。 UIWebView 子类有一个启动并显示活动指示器。在 WebViewDidStartLoad 和 WebViewDidFinishLoad 上停止动画,效果很好,但它对“白色闪光”没有任何帮助。

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 backgrounds, 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."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你怎么敢 2024-11-14 03:04:41

我已经测试过了...我正在发送我使用过的两种方法...

 - (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];
}

这里我在 0.1 秒后执行请求,否则它会像您的情况一样看起来是白色的。或者您可以根据时间给出延迟时间..干杯:)

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 :)

蓝天 2024-11-14 03:04:41

尝试在你的

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

然后在

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

Try in your

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

Then in

    -(void)loadURL:(id)sender{  
        myWebView.hidden = NO;
水染的天色ゝ 2024-11-14 03:04:41

我使用:

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

在创建 webView 并发出请求时,然后添加此委托方法来处理已完成的请求:

- (void) webViewDidFinishLoad:(UIWebView*)webView{  
    [webView setHidden: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];  
}
夜未央樱花落 2024-11-14 03:04:41

成功地做到了。
您必须首先在 Interface Builder 中创建一个视图。
然后使用包含 webview 的 ViewController 的 init 中的 initWithFrame 将 html 加载到 webview(这就是神奇发生的地方):

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

然后简单地将 webView 加载到 viewWillAppear 中的视图中:

[viewWeb addSubview:webView];

这实际上是一个界面设计问题,其中直接在视图上绘制或在子视图中绘制然后在视图中绘制该子视图更快吗?

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?

水晶透心 2024-11-14 03:04:41

我几年前就解决了这个问题,使用常见的方法将 UIWebView 隐藏在 UIImageView 后面,然后在延迟后删除 UIImageView。

但它突然又回来了,我想是在 iOS 7.0.4 上。该问题发生在全新的 iPad Air 以及较旧的非视网膜版 iPad mini 上。经过两天的折腾,终于找到了解决办法。

假设您的 webview 仅限于横向,初始化如下:

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

然后在预加载后使其可见,例如在 webview 上使用bringSubviewToFront或setHidden:NO(或者在UIImageView上使用setHidden:YES或removeFromSuperview) 。但不是无缝切换视图,而是闪烁并且背景颜色闪烁大约半秒。

解决方法是稍微改变你的 webview 的大小:

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

问题和修复是高度可重现的。它的工作原理是小数点后第四位 (1024.0001f) 的微小变化。在小数点第五位 (1024.00001f) 处,闪光灯返回。高度的值(768.0f)似乎并不重要。

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.

怪我太投入 2024-11-14 03:04:41

或者,您可以将 webview 的背景颜色设置为您正在使用的任何背景颜色,而不是解决该问题。当然,除非您使用图像。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文