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

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

    Swift 5.x Version

    import UIKit

    class SKLoader: NSObject {
        
        static let sharedInstance = SKLoader()
    
        let indicator: UIActivityIndicatorView? = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.medium)
    
        let screen = UIScreen.main.bounds
    
        var appDelegate: SceneDelegate {
            guard let sceneDelegate = UIApplication.shared.connectedScenes
                .first!.delegate as? SceneDelegate else {
                    fatalError("sceneDelegate is not UIApplication.shared.delegate")
            }
            return sceneDelegate
        }
        
        var rootController:UIViewController? {
            guard let viewController = appDelegate.window?.rootViewController else {
                fatalError("There is no root controller")
            }
            return viewController
        }
        
        func show() {
            indicator?.frame = CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0)
            indicator?.frame.origin.x = (screen.width/2 - 20)
            indicator?.frame.origin.y = (screen.height/2 - 20)
            rootController?.view.addSubview(indicator!)
            indicator?.startAnimating()
        }
        
        func hide() {
            DispatchQueue.main.async {
                self.indicator?.stopAnimating()
                self.indicator?.removeFromSuperview()
            }
        }
    }
    

    Sample Usage:

    //To Show

    SKLoader.sharedInstance.show()

    //To Hide

    SKLoader.sharedInstance.hide()

    =======

提交回复
热议问题