Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically add a simple default loading(progress) bar in iphone app

Tags:

ios

iphone

UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
indicator.center = self.view.center;    
[self.view addSubview:indicator];
[indicator bringSubviewToFront:self.view];
[UIApplication sharedApplication].networkActivityIndicatorVisible = TRUE;

Write below code when you want to show indicator

[indicator startAnimating];

write below code when you want to hide indicator

[indicator stopAnimating];

Translating @Hiren's answer to Swift

 var indicator: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
 indicator.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
 indicator.center = view.center
 self.view.addSubview(indicator)
 self.view.bringSubview(toFront: indicator)
 UIApplication.shared.isNetworkActivityIndicatorVisible = true

Show indicator

indicator.startAnimating()

Stop indicator

indicator.stopAnimating()

I'd recommend using NSURLConnection. The methods you would need are:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  [self.resourceData setLength:0];
  self.filesize = [NSNumber numberWithLongLong:[response expectedContentLength]];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
  [self.resourceData appendData:data];
  NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[self.resourceData length]];
  self.progressBar.progress = [resourceLength floatValue] / [self.filesize floatValue];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
  self.progressBar.hidden = YES;
}

And the header file:

@property (nonatomic, retain) UIProgressView *progressBar;
@property (nonatomic, retain) NSMutableData *resourceData;
@property (nonatomic, retain) NSNumber *filesize;

Hope it helps


To maintain this question totally updated I have translated @enrique7mc's answer to Swift3.0 following his translation from @Hiren's answer.

var indicator: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
indicator.frame = CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0)
indicator.center = view.center
view.addSubview(indicator)
indicator.bringSubview(toFront: view)
UIApplication.shared.isNetworkActivityIndicatorVisible = true

To start and stop the progress bar is in the same way that @enrique7mc pointed out.

indicator.startAnimating()
indicator.stopAnimating()

UIProgressView is the class you are looking for: Apple Docs

You need to use the setProgress:animated: method to updated the shown progress. Most likely where you handle received data from the network.


You can use IOS in-built UIProgressView. Below is code snippet:

UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
[self.view addSubview:progressView];
[progressView setProgress:50.0];

You can use setFrame: for positioning of progress bar on view.


Try below code

float progress;

//components
UIProgressView *progressBar;
progressBar=[[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
[progressBar setFrame:CGRectMake(30.0, 75.0, 200.0, 80.0)];

int prog=progress*100;
progressStr=[NSString stringWithFormat:@"%d%%",prog];
[progressBar setProgress:progress];