如何以编程方式在iPhone应用程序中添加一个简单的默认加载(进度)栏

39 iphone ios

我在我的iPhone应用程序中使用http通信.我想在从服务器加载数据时显示进度条.我该如何以编程方式完成?

我只想要一个默认的进度条.没有什么花哨的东西,比如我们做的机器人ProgressDialog.show();,是否有任何一个衬垫在iphone中显示进度条?

Hir*_*ren 81

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;
Run Code Online (Sandbox Code Playgroud)

如果要显示指标,请在下面写下代码

[indicator startAnimating];
Run Code Online (Sandbox Code Playgroud)

如果要隐藏指标,请在下面写下代码

[indicator stopAnimating];
Run Code Online (Sandbox Code Playgroud)

  • 也不要忘记停止网络活动指示器`[UIApplication sharedApplication] .networkActivityIndi​​catorVisible = FALSE;` (6认同)
  • 人们看到这个伟大的答案,别忘了在第一行的开头添加UIActivityindicatorView而不是UIActivityindicator (2认同)

enr*_*7mc 36

将@ Hiren的答案翻译成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
Run Code Online (Sandbox Code Playgroud)

显示指标

indicator.startAnimating()
Run Code Online (Sandbox Code Playgroud)

停止指示器

indicator.stopAnimating()
Run Code Online (Sandbox Code Playgroud)


Nov*_*arg 11

我建议使用NSURLConnection.您需要的方法是:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

和头文件:

@property (nonatomic, retain) UIProgressView *progressBar;
@property (nonatomic, retain) NSMutableData *resourceData;
@property (nonatomic, retain) NSNumber *filesize;
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你


Fra*_*ero 9

为了保持这个问题的完全更新,我已经将@ enrique7mc的答案翻译成了@Hiren Swift3.0的回答.

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
Run Code Online (Sandbox Code Playgroud)

启动和停止进度条与@ enrique7mc指出的方式相同.

indicator.startAnimating()
indicator.stopAnimating()
Run Code Online (Sandbox Code Playgroud)


Eng*_*epe 6

UIProgressView是您正在寻找的课程:Apple Docs

您需要使用该setProgress:animated:方法更新显示的进度.最有可能处理从网络接收的数据.


小智 6

您可以使用IOS内置UIProgressView.以下是代码段:

UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
[self.view addSubview:progressView];
[progressView setProgress:50.0];
Run Code Online (Sandbox Code Playgroud)

您可以使用setFrame:在视图上定位进度条.


Kul*_*eep 5

试试下面的代码

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];
Run Code Online (Sandbox Code Playgroud)