UIWebView:加载时显示图像
在我的故事板中,我有一个带有 UIWebView 的视图,它填充了除选项卡和导航栏之外的整个区域。视图的控制器实现了 UIWebViewDelegate 协议。控制器被设置为 UIWebView 的委托:
#pragma mark - UIWebViewDelegate
- (void) webViewDidStartLoad:(UIWebView *)webView {
NSLog(@"webViewDidStartLoad");
}
- (void) webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"webViewDidFinishLoad");
}
当 UIWebView 加载时,我将显示一个黑色背景,并显示一个图像,表明它正在加载。我怎样才能做到这一点?
编辑1: 我正在尝试添加默认的 ActivityIndicatorView 而不是静态图像。但这只是给了我一个带有小白色区域的黑色背景。问题是什么?
UIImageView *loadingImageView = [[UIImageView alloc] initWithFrame:webView.frame];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
UIImage *spacer = [UIImage imageNamed:@"spacer"];
UIGraphicsBeginImageContext(spinner.frame.size);
[spacer drawInRect:CGRectMake(0,0,spinner.frame.size.width,spinner.frame.size.height)];
UIImage* resizedSpacer = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
loadingImageView.image = resizedSpacer;
[loadingImageView setBackgroundColor:[UIColor blackColor]];
[loadingImageView setContentMode:UIViewContentModeCenter];
self.loadingImageView = loadingImageView;
In my storyboard I have a view with a UIWebView which is filling the entire area except from tab and navigation bar. The controller for the view is implements the protocol UIWebViewDelegate. The controller is set as delegate for the UIWebView:
#pragma mark - UIWebViewDelegate
- (void) webViewDidStartLoad:(UIWebView *)webView {
NSLog(@"webViewDidStartLoad");
}
- (void) webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"webViewDidFinishLoad");
}
While the UIWebView is loading I'll show an black background with an image stating that it is loading. How can I do that?
EDIT1:
I'm trying to add the default ActivityIndicatorView
instead of a static image. But this just gives me a black background with a small white area. What is the problem?
UIImageView *loadingImageView = [[UIImageView alloc] initWithFrame:webView.frame];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
UIImage *spacer = [UIImage imageNamed:@"spacer"];
UIGraphicsBeginImageContext(spinner.frame.size);
[spacer drawInRect:CGRectMake(0,0,spinner.frame.size.width,spinner.frame.size.height)];
UIImage* resizedSpacer = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
loadingImageView.image = resizedSpacer;
[loadingImageView setBackgroundColor:[UIColor blackColor]];
[loadingImageView setContentMode:UIViewContentModeCenter];
self.loadingImageView = loadingImageView;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在你的控制器 .h 中
在你的控制器 .m
编辑 1:
如果你想显示 ActivityIndicatorView 而不是静态图像,这里有一个我喜欢使用的小帮助器类:
LoadingIndicator .h
LoadingIndicator.m
然后在您的视图中。h
在您的视图中。m
最后在您的 WebView 委托中:
这将如下所示:
In your controller .h
In your controller .m
EDIT 1:
If you would like to show an ActivityIndicatorView instead of a static image, here is a little helper class that I love to use:
LoadingIndicator.h
LoadingIndicator.m
Then in your view.h
In your view.m
Finally in your WebView delegate:
This will look something like this:
您只需在 UIWebViewDelegate 方法中隐藏和取消隐藏包含所需消息的加载图像即可。
You just hide and unhide the loading image containing the message you want in the UIWebViewDelegate methods.