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

前端 未结 18 2528
你的背包
你的背包 2020-12-12 17:16

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?

I just want a d

相关标签:
18条回答
  • 2020-12-12 17:43

    SwiftUI version

    Very crude and simple, not-networked, basic progress indicator. Still not supported by default in SwiftUI, so the UIViewRepresentable is the way to go. Enjoy modifying it to your heart's contents.

    struct ActivityIndicatorView: UIViewRepresentable {
        let large: Bool
        @State var enabled: Bool
    
        func makeUIView(context: Context) -> UIActivityIndicatorView {
            let view = UIActivityIndicatorView(style: large ? .large : .medium)
            return view
        }
    
        func updateUIView(_ view: UIActivityIndicatorView, context: Context) {
            if enabled {
                view.startAnimating()
            } else {
                view.stopAnimating()
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-12 17:49

    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

    0 讨论(0)
  • 2020-12-12 17:49

    Swift 3 version of @enrique7mc solution

        var indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
        indicator.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
        indicator.center = view.center
        view.addSubview(indicator)
        indicator.bringSubview(toFront: view)
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
    
    
        indicator.startAnimating()
        indicator.stopAnimating()
    
    0 讨论(0)
  • 2020-12-12 17:50

    Swift 5.x version of @enrique7mc's Swift code:

    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.

    0 讨论(0)
  • 2020-12-12 17:50

    @hiren's answer translation to Swift4.0

    let indicator: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: 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
    
    
    indicator.startAnimating()
    indicator.stopAnimating()
    
    0 讨论(0)
  • 2020-12-12 17:52

    App Delegate.h

    -(void)showLoader;
    
    -(void)hideLoder;
    

    App Delegate.m

    @implementation AppDelegate
    
    AppDelegate *app;
    
    -(void)showLoader
    {
    
        if(loaderView== NULL)
        {
            loaderView=[[UIView alloc] initWithFrame:self.window.frame];
    
            [loaderView setBackgroundColor:[UIColor blackColor]];
    
            [loaderView setAlpha:0.5];
    
            spinner = [[UIActivityIndicatorView alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    
            [loaderView addSubview:spinner];
    
            spinner.center = CGPointMake(loaderView.frame.size.width/2-10, 
    
            loaderView.frame.size.height/2-10);
    
            spinner.hidesWhenStopped = YES;  
        }
        [spinner startAnimating];
        [self.window addSubview:loaderView];
        [self.window bringSubviewToFront:spinner];
        [self.window bringSubviewToFront:loaderView];
    }
    
    -(void)hideLoder
    {       
        if (spinner!= NULL) {           
            [spinner stopAnimating];
        }
    
        [loaderView removeFromSuperview];
    }
    

    now import "AppDelegate.h" class where ever you want to call loader.and you can call the loader like this.

    [app showLoader];

    and

    [app hideLoder];

    0 讨论(0)
提交回复
热议问题