在 uiwebview 显示本地 HTML 页面之前几毫秒出现空白屏幕
我有一个显示表视图的 UINavigationController。当我单击一行时,我会在堆栈上推送一个包含 UIWebView 的新视图。 在视图控制器内,我在 UIWebView 中加载本地 HTML 页面。它工作正常,除了第一次启动应用程序时显示 UIWebView 时,会出现一个空白的白色屏幕,在真正显示 HTML 内容之前 1 秒内会出现该屏幕。 下次也会显示空白屏幕,但只有几毫秒。
在显示 HTML 之前,如何避免出现空白屏幕? 这是我用来在 uiWebView 中显示 HTML 文件的代码。
NSString *localizedAboutHTML = NSLocalizedString(@"About", @"Filename of the HTML page to be displayed depending on the language");
NSString *path = [[NSBundle mainBundle] pathForResource:localizedAboutHTML ofType:@"html"];
NSURL* baseURL = [NSURL fileURLWithPath:path];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *htmlString = [[NSString alloc] initWithData:
[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
webView.opaque = YES;
webView.backgroundColor = [UIColor blackColor];
[self.webView loadHTMLString:htmlString baseURL:baseURL];
[super viewWillAppear:animated];
谢谢,
塞布。
I have a UINavigationController that display a table view. When I click on a row then I push a new view on the stack that contains a UIWebView.
Inside the View Controller I load a local HTML page in the UIWebView. It works fine except that the first time the app is started when the UIWebView is displayed there is a blank white screen that is pushed during 1 sec before to really display the HTML content.
Next times the blank white screen is displayed also but only few millisecond.
How can I do to avoid this blank white screen before the HTML is displayed?
Here's the code I'm using to display the HTML file in the uiWebView.
NSString *localizedAboutHTML = NSLocalizedString(@"About", @"Filename of the HTML page to be displayed depending on the language");
NSString *path = [[NSBundle mainBundle] pathForResource:localizedAboutHTML ofType:@"html"];
NSURL* baseURL = [NSURL fileURLWithPath:path];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *htmlString = [[NSString alloc] initWithData:
[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
webView.opaque = YES;
webView.backgroundColor = [UIColor blackColor];
[self.webView loadHTMLString:htmlString baseURL:baseURL];
[super viewWillAppear:animated];
Thanks,
Séb.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为 WebView 加载可能需要更长的时间,具体取决于您的 HTML 文件。如果您不想在短时间内显示白屏,为什么不添加活动指示器以显示 webview 正在为用户加载。另一种选择是隐藏 webView 并在 webView 加载完成后在 webview 委托方法中显示它
webViewDidFinishLoad:(UIWebView *)webView
Because WebView loading can take longer, depends on your HTML file. If you don't want to show white screen appears for that short period of time why not Add activity indicator to show that webview is loading for users. One more option is hide webView and show it after webView loading is complete in webview delegate method
webViewDidFinishLoad:(UIWebView *)webView