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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9976278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:40:24  来源:igfitidea点击:

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

iphoneios

提问by Hiren

I am using http communication in My iPhone app. I want to show a progress bar while it is loading data from server. How can I do it programmatically?

我在我的 iPhone 应用程序中使用 http 通信。我想在从服务器加载数据时显示进度条。我怎样才能以编程方式做到这一点?

I just want a default progress-bar. Nothing fancy, like in android we do ProgressDialog.show();, is there any one liner for showing progress-bar in iphone?

我只想要一个默认的进度条。没什么特别的,就像我们在 android 中所做的那样ProgressDialog.show();,有没有一种在 iphone 中显示进度条的衬垫?

回答by Hiren

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];

回答by enrique7mc

Translating @Hiren's answer to Swift

翻译@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

Show indicator

显示指示器

indicator.startAnimating()

Stop indicator

停止指示灯

indicator.stopAnimating()

回答by Novarg

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

我建议使用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;
}

And the header file:

和头文件:

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

Hope it helps

希望能帮助到你

回答by Francisco Romero

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

为了保持这个问题完全更新,我已经将@enrique7mc 的回答翻译成了@HirenSwift3.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

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

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

indicator.startAnimating()
indicator.stopAnimating()

回答by Engin Kurutepe

UIProgressViewis the class you are looking for: Apple Docs

UIProgressView是您正在寻找的课程: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.

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

回答by Jeet

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

您可以使用 IOS 内置的 UIProgressView。下面是代码片段:

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.

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

回答by Kuldeep

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];

回答by the_dude_abides

I know this is an older question but I've written a global static method for doing this type of thing from any view, based on the above answers.

我知道这是一个较旧的问题,但我已经根据上述答案编写了一个全局静态方法,用于从任何角度执行此类操作。

Main updates are:

主要更新有:

  • support optional opaque overlay behind the activity indicator
  • use default frame sizing for the activity indicator
  • use .WhiteLarge indicator style
  • 支持活动指示器后面的可选不透明覆盖
  • 为活动指示器使用默认帧大小
  • 使用 .WhiteLarge 指示器样式

In my AppHelper.swift:

在我的 AppHelper.swift 中:

static func showActivityIndicator(view: UIView, withOpaqueOverlay: Bool) {

    // this will be the alignment view for the activity indicator
    var superView: UIView = view

    // if we want an opaque overlay, do that work first then put the activity indicator within that view; else just use the passed UIView to center it
    if withOpaqueOverlay {
        let overlay = UIView()
        overlay.frame = CGRectMake(0.0, 0.0, view.frame.width, view.frame.height)
        overlay.layer.backgroundColor = UIColor.blackColor().CGColor
        overlay.alpha = 0.7
        overlay.tag = activityIndicatorOverlayViewTag

        overlay.center = superView.center
        overlay.hidden = false
        superView.addSubview(overlay)
        superView.bringSubviewToFront(overlay)

        // now we'll work on adding the indicator to the overlay (now superView)
        superView = overlay
    }

    let indicator: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge)

    indicator.center = superView.center
    indicator.tag = activityIndicatorViewTag
    indicator.hidden = false

    superView.addSubview(indicator)
    superView.bringSubviewToFront(indicator)

    indicator.startAnimating()

    // also indicate network activity in the status bar
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}

static func hideActivityIndicator(view: UIView) {

    // stop the network activity animation in the status bar
    UIApplication.sharedApplication().networkActivityIndicatorVisible = false

    // remove the activity indicator and optional overlay views
    view.viewWithTag(activityIndicatorViewTag)?.removeFromSuperview()
    view.viewWithTag(activityIndicatorOverlayViewTag)?.removeFromSuperview()
}

回答by Neph

Swift 5.xversion of @enrique7mc's Swift code:

@enrique7mc 的 Swift 代码的Swift 5.x版本:

var indicator: UIActivityIndicatorView = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.gray)
indicator.frame = CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0)
indicator.center = view.center
view.addSubview(indicator)
indicator.bringSubviewToFront(view)
UIApplication.shared.isNetworkActivityIndicatorVisible = true

Start it with

开始吧

indicator.startAnimating()

Stop it with

停止它

indicator.stopAnimating()

This adds a small ActivityIndicator(turning circle, Apple's documentation) to the middle of the screen and to the status bar. The first one will be cleared once you switch to a different ViewController, while the one in the status bar won't - you have to manually disable it.

这会在屏幕中间和状态栏上添加一个小ActivityIndicator(旋转圆圈,Apple 的文档)。一旦您切换到不同的ViewController,第一个将被清除,而状态栏中的则不会 - 您必须手动禁用它。

回答by Hlung

Try MBProgressHUD.

试试MBProgressHUD

It's pretty simple, got several progress animation options, and able to add some customization. It displays fullscreen. And it should work with any recent iOS versions.

这是很简单的,有几个进度动画选项,可以添加一些定制。它显示全屏。它应该适用于任何最新的 iOS 版本。

Or try LDProgressViewif you want something more fancy :) It somehow looks like OSX progress bar.

或者,如果您想要更奇特的东西,请尝试LDProgressView:) 它看起来像 OSX 进度条。