本文介绍了将 HTML 字符串加载到 UIWebView 时出现延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在导航控制器中有两个视图控制器.第一个视图控制器有一个带按钮的菜单.按下这个按钮会移动到第二个视图控制器并将一个 html 字符串加载到 UIWebView 中.没有其他东西被加载到 webview,只是一个简单的 NSString 和 html 代码.本质上,我正在制作一张翻转卡片(两个视图),其中 webview 作为其中一个 uiview 的子视图.

I have two view controllers inside a navigation controller. The 1st view controller has a menu with a button. Pressing this button moves to the 2nd view controller and loads a html string into a UIWebView. Nothing else is being loaded to the webview, just a simple NSString with html code in it. Essentially I'm making a flip card (two views) with the webview as a subview of one of the uiviews.

代码如下:

containerView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 280, 280)];
[self.view addSubview:containerView];

frontView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 280)];
frontView.layer.cornerRadius = 10;
frontView.layer.masksToBounds = YES;
[frontView setBackgroundColor:[UIColor whiteColor]];
backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 280)];
backView.layer.cornerRadius = 10;
backView.layer.masksToBounds = YES;
[backView setBackgroundColor:[UIColor yellowColor]];
[containerView addSubview:frontView];

webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 280, 280)];
webView.scalesPageToFit = YES;
webView.userInteractionEnabled = NO;

NSString *htmlString = @"<head><meta name='viewport' content='width=device-width;'><style type='text/css'>html,body {margin: 0;padding: 0;width: 100%;height: 100%;}html {display: table;}body {display: table-cell;vertical-align: middle;padding: 20px;text-align: center;-webkit-text-size-adjust: none;}</style></head><ruby>金<rt>きん</rt></ruby><ruby>曜<rt>よう</rt></ruby><ruby>日<rt>び</rt></ruby>";

[webView loadHTMLString:htmlString baseURL:nil];
[frontView addSubview:webView];

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                      action:@selector(flip)];
[self.view addGestureRecognizer:tap];

}

问题:我注意到在我第一次按下按钮转换到第二个视图控制器时,看到加载到 webview 的 html 字符串有延迟(只有大约 0.5 - 1 秒).如果我回到第一个视图控制器并再次点击按钮,则不会发生这种情况 - 这次是即时的.

Issue: I notice that on the first time I press the button to transition to the second view controller that there is a delay in seeing the html string loaded to the webview (it's only about 0.5 - 1 second). This doesn't happen if I go back to the first view controller and hit the button again - this time it's instant.

任何想法可能导致第一次延迟以及如何避免它?

Any ideas what might be causing this first time delay and how to avoid it?

谢谢

推荐答案

加载 html 字符串导致延迟是一个尚未修复的已知问题

Loading the html string causing the delay is a known issue not yet fixed

您可以尝试包含一个活动指示器并显示它隐藏 webview 并为其设置动画直到 webview 完全加载.这可以通过 UIWebview 的委托来实现.加载完成后取消隐藏 webview 并删除活动

what you could try is to include an activity indicator and show it hiding the webview and animate it until webview loads completely.This can be achieved by the delegats of UIWebview.And when loading is completed unhide the webview and remove the activity

使用

-(void)webViewDidFinishLoad:(UIWebView *)webView ;
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;

这篇关于将 HTML 字符串加载到 UIWebView 时出现延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 00:38