让UIWebView正确加载到CGRect中

时间:2011-03-14 21:09:21

标签: iphone uiwebview uiactivityindicatorview

我正在尝试让我的webView在我设置的CGRect框架中正确加载,并且activityIndi​​cator正在运行。

当我使用[self.view addSubview:webView];设置它时,它正确加载,但是activityindicator没有出现。当我使用webView.delegate = self;进行设置时,会显示activityindicator,但是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;
}

1 个答案:

答案 0 :(得分:2)

应该是

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"activity stopped");
    [activityIndicator stopAnimating];
    [[self view]  addSubView:webView];
}

视图未被覆盖,通过重置视图属性将其从层次结构中删除。