简体   繁体   中英

Activity Indicator doesn't disappear when webView finishes loading

I am developing iOS App with Objective-c. Then, I am implementing customized Activity Indicator which is active while webView is loading.

The customized Activity Indicator appears when webView starts loading, however it does not disappear when webView finishes loading.

My code is following.

Could you tell me how to solve this problem?

@interface DetailViewController ()<UIWebViewDelegate>{
{
UILabel *loadingLabel;
UIActivityIndicatorView *loadingMark;
UIView *loadingContainer;
}
@property (weak, nonatomic) IBOutlet UIWebView *detailPage;
@end

@implementation DetailViewController

- (void)configureView
{
    NSURL *page_url = [NSURL URLWithString:@"http://yahoo.co.jp"];
    NSURLRequest *request = [NSURLRequest requestWithURL:page_url];
    [_detailPage loadRequest:request];
    _detailPage.delegate = self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
[self configureView];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)webViewDidStartLoad:(UIWebView*)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[self animatingIndicator];
}


- (void)webViewDidFinishLoad:(UIWebView*)webView
{
[self stopAndRemoveIndicator];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;  
}

-(void)animatingIndicator {

loadingContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 60)];

//Set Style Of Label
loadingLabel = [[UILabel alloc] init];
loadingLabel.text = @"Loading";
loadingLabel.textColor = [UIColor whiteColor];
loadingLabel.shadowColor = [UIColor blackColor];
loadingLabel.shadowOffset = CGSizeMake(1, 1);
loadingLabel.font = [UIFont boldSystemFontOfSize:17];
loadingLabel.backgroundColor = [UIColor lightGrayColor];
loadingLabel.frame = CGRectMake(20, 17, 70, 25);
[loadingContainer addSubview:loadingLabel];

//Set Style Of Mark
loadingMark = [[UIActivityIndicatorView alloc]    initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
loadingMark.frame = CGRectMake(100, 14, 30, 30);
[loadingContainer addSubview:loadingMark];

//Set Style Of Container
loadingContainer.backgroundColor = [UIColor lightGrayColor];
[[loadingContainer layer] setCornerRadius:8.0f];
[[loadingContainer layer] setMasksToBounds:YES];
[[loadingContainer layer] setBorderWidth:0.5f];
[[loadingContainer layer] setBorderColor:[[UIColor darkGrayColor] CGColor]];
CGRect rect = self.view.frame;
loadingContainer.center = CGPointMake(rect.size.width/2, rect.size.height/2 * 0.8);
[self.view addSubview:loadingContainer];

[loadingMark startAnimating]; 
loadingContainer.hidden = NO; 
}

-(void)stopAndRemoveIndicator {
[loadingMark stopAnimating];
loadingContainer.hidden = YES;
[loadingMark setHidden:YES];
}

You need to hide the UIKit objects in the main thread.

override func webViewDidFinishLoad(webView:UIWebView)
{
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
      self.loadingSign.stopAnimating()
      self.loadingSign.hidden = true
    }
} 

Update for swift 4

override func webViewDidFinishLoad(_ webView: UIWebView)
{
    DispatchQueue.main.async {
      self.loadingSign.stopAnimating()
      self.loadingSign.hidden = true
    }
}

loadingSign is UIActivity Indicator. This is what I have done, work fine!

.h file @property (retain, nonatomic) IBOutlet UIActivityIndicatorView *loadingSign;

.m file

-(void)webViewDidStartLoad:(UIWebView *)webView
{
    [self.loadingSign startAnimating];
    self.loadingSign.hidden = NO;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.loadingSign stopAnimating];
    self.loadingSign.hidden = YES;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM