Ios 将HTML字符串加载到UIWebView时出现延迟

Ios 将HTML字符串加载到UIWebView时出现延迟,ios,objective-c,performance,uiwebview,uinavigationcontroller,Ios,Objective C,Performance,Uiwebview,Uinavigationcontroller,我在导航控制器中有两个视图控制器。第一视图控制器有一个带有按钮的菜单。按下此按钮将移动到第二视图控制器,并将html字符串加载到UIWebView中。没有其他内容加载到webview,只是一个简单的NSString,其中包含html代码。基本上,我正在制作一个翻转卡(两个视图),其中webview是其中一个UIView的子视图 代码如下: containerView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 280, 280)]; [s

我在导航控制器中有两个视图控制器。第一视图控制器有一个带有按钮的菜单。按下此按钮将移动到第二视图控制器,并将html字符串加载到UIWebView中。没有其他内容加载到webview,只是一个简单的NSString,其中包含html代码。基本上,我正在制作一个翻转卡(两个视图),其中webview是其中一个UIView的子视图

代码如下:

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];
containerView=[[UIView alloc]initWithFrame:CGRectMake(20,20280280)];
[self.view addSubview:containerView];
frontView=[[UIView alloc]initWithFrame:CGRectMake(0,0280280)];
frontView.layer.cornerRadius=10;
frontView.layer.masksToBounds=是;
[frontView setBackgroundColor:[UIColor whiteColor]];
backView=[[UIView alloc]initWithFrame:CGRectMake(0,0280280)];
backView.layer.cornerRadius=10;
backView.layer.masksToBounds=是;
[backView setBackgroundColor:[UIColor yellowColor];
[containerView添加子视图:frontView];
webView=[[UIWebView alloc]initWithFrame:CGRectMake(0,0280280)];
webView.scalesPageToFit=是;
webView.userInteractionEnabled=否;
NSString*htmlString=@“html,正文{边距:0;填充:0;宽度:100%;高度:100%;}html{显示:表格;}正文{显示:表格单元格;垂直对齐:中间;填充:20px;文本对齐:中心;-webkit文本大小调整:无;}金きん曜よう日び";
[webView加载htmlString:htmlString基URL:nil];
[frontView添加子视图:网络视图];
UITapGestureRecognizer*点击=[[UITapGestureRecognizer alloc]initWithTarget:self
动作:@选择器(翻转)];
[self.view addgesturecognizer:tap];
}

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

你知道是什么导致了第一次延误,以及如何避免延误吗


感谢

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

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

使用


请取消选中UIWebView中的电话号码检测功能。

另一种解决方案是从
UIWebView
切换到
-[UIButton setAttributedTitle:forState::

[[NSAttributedString alloc] initWithData:[@"<head><meta ... </ruby>" dataUsingEncoding:NSUTF8StringEncoding]
                                 options:@{
                                           NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding),
                                           NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType
                                          }
                      documentAttributes:NULL
                                   error:NULL];

[[NSAttributedString alloc]initWithData:[@”谢谢你的建议。我不知道这是一个普遍存在的问题。
[[NSAttributedString alloc] initWithData:[@"<head><meta ... </ruby>" dataUsingEncoding:NSUTF8StringEncoding]
                                 options:@{
                                           NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding),
                                           NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType
                                          }
                      documentAttributes:NULL
                                   error:NULL];