我有一个 viewDidLoad,它创建一个 Web View 并启动一个事件指示器。如何让事件指示器仅在网页出现时停止?如何向事件指示器添加单词,例如“正在加载”?
// Create the UIWebView
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:webView];
// Start the throbber to check if the user exists
UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityView.center = CGPointMake([self screenWidth] / 2.0, 370.0);
[activityView startAnimating];
activityView.tag = 100;
[self.view addSubview:activityView];
NSString* url = @"http://google.com";
NSURL* nsUrl = [NSURL URLWithString:url];
NSURLRequest* request = [NSURLRequest requestWithURL:nsUrl cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30];
// Remove activity view
UIView *viewToRemove = [self.view viewWithTag:100];
//[viewToRemove removeFromSuperview];
[webView loadRequest:request];
最佳答案
制作加载 View :
@implementation yourViewController{;
UIView* loadingView;
}
在你的viewDidLoad
:
loadingView = [[UIView alloc]initWithFrame:CGRectMake(100, 400, 80, 80)];
loadingView.backgroundColor = [UIColor colorWithWhite:0. alpha:0.6];
loadingView.layer.cornerRadius = 5;
UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityView.center = CGPointMake(loadingView.frame.size.width / 2.0, 35);
[activityView startAnimating];
activityView.tag = 100;
[loadingView addSubview:activityView];
UILabel* lblLoading = [[UILabel alloc]initWithFrame:CGRectMake(0, 48, 80, 30)];
lblLoading.text = @"Loading...";
lblLoading.textColor = [UIColor whiteColor];
lblLoading.font = [UIFont fontWithName:lblLoading.font.fontName size:15];
lblLoading.textAlignment = NSTextAlignmentCenter;
[loadingView addSubview:lblLoading];
[self.view addSubview:loadingView];
这个 View 看起来像这样:
注意,如果你想使用cornerRadius,你必须导入<QuartzCore/QuartzCore.h>
框架,当然,在导入之前,添加 QuartzCore
您项目的框架!
检测 webview 何时停止加载:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[loadingView setHidden:YES];
}
你必须执行 UIWebViewDelegate
协议(protocol),和
webView.delegate = self;
并使其可见:
- (void)webViewDidStartLoad:(UIWebView *)webView {
[loadingView setHidden:NO];
}
关于ios - 在 uiwebview 上添加事件指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19475350/