Show Loader or Activity indicator in swift

Set following variables in your code

var activityView: UIView = UIView()
var loadingView: UIImageView = UIImageView()
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()

Set following method in your code for show and hide activity indicator

    func showActivity(_ myView: UIView) {
        
        self.activityView.frame = myView.frame
        self.activityView.center = myView.center
        self.activityView.backgroundColor = UIColorFromHex(rgbValue: 0xffffff, alpha: 0.5)
        
        loadingView.frame = CGRect(x: 0, y: 0, width: 90, height: 90)
        loadingView.center = myView.center
        let jeremyGif = UIImage.gifImageWithName("loading")
        loadingView.image = jeremyGif
        
        
        activityIndicator.frame = CGRect(x: 0, y: 0, width: 45, height: 45)
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
        activityIndicator.center = CGPoint(x: self.loadingView.frame.size.width / 2, y: loadingView.frame.size.height / 2);
        
        //  loadingView.addSubview(activityIndicator)
        self.activityView.addSubview(loadingView)
        myView.addSubview(self.activityView)
        activityIndicator.startAnimating()
        
    }
    
    func UIColorFromHex(rgbValue:UInt32, alpha:Double=1.0)->UIColor {
        let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0
        let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0
        let blue = CGFloat(rgbValue & 0xFF)/256.0
        return UIColor(red:red, green:green, blue:blue, alpha:CGFloat(alpha))
    }
    
    func removeActivity(_ myView: UIView) {
        activityIndicator.stopAnimating()
        self.activityView.removeFromSuperview()
    }