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

前端 未结 18 2550
你的背包
你的背包 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:39

    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

    In my 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()
    }
    

提交回复
热议问题