我试图让我的 webView 在我设置的 CGRect 框架中正确加载,同时运行 activityIndicator。
当我设置它[self.view addSubview:webView];
但没有出现活动指示器时,它可以正确加载。当我用activityindicator设置它时,webView.delegate = self;
确实出现了,但是webview在加载时会覆盖整个屏幕。我在顶部有一个分段条,然后被掩盖。
- (void)viewDidLoad {
[super viewDidLoad];
CGRect webFrame = CGRectMake(0.0, 50.0, 320.0, 318.0);
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[self.view addSubview:activityIndicator];
activityIndicator.frame = CGRectMake(250, 250, 30.0, 30.0);
self.view.center = CGPointMake(160.0f, 190.0f);
activityIndicator.center = self.view.center;
webView = [[UIWebView alloc] initWithFrame:webFrame];
[webView setBackgroundColor:[UIColor whiteColor]];
NSString *urlAddress = @"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
webView.delegate = self;
// [self.view addSubview:webView];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(@"activity started");
[activityIndicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"activity stopped");
[activityIndicator stopAnimating];
self.view = webView;
}