让 UIWebView 正确加载到 CGRect 中

发布于 2024-10-22 02:14:31 字数 1383 浏览 3 评论 0原文

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

当我使用 [self.view addSubview:webView]; 设置它时,它可以正确加载,但活动指示器没有出现。当我使用 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;
}

I'm trying to get my webView to load correctly in the CGRect frame I set up, with the activityIndicator running.

It kind of loads correctly when I have it set up with [self.view addSubview:webView]; but the activityindicator doesnt appear. When I set it up with webView.delegate = self;the activityindicator does appear, but the webview covers up the whole screen when it loads up. I have a segmentedbar up top that gets covered up then.

- (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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别在捏我脸啦 2024-10-29 02:14:31

应该是

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

视图没有被覆盖,它们是通过重置视图属性从层次结构中删除的。

It should be

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

The views weren't being covered, they were being removed from the hierarchy by resetting the view property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文