iosswiftswift3uiactivityindicatorview

Activity indicator is not in centered in TableView


I want show activity indicator in TableView with NIB/XIB file, but it shown is not in center, it placed in right side

I do:

override func viewDidLoad() {
    super.viewDidLoad()

let actInd: UIActivityIndicatorView = UIActivityIndicatorView()
    actInd.frame = CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0)
    actInd.center = self.view.center
    actInd.hidesWhenStopped = true
    actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
    self.view.addSubview(actInd)
    actInd.startAnimating()
}

Solution

  • let actInd: UIActivityIndicatorView = UIActivityIndicatorView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        actInd.frame = CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0)
        actInd.hidesWhenStopped = true
        actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
        self.view.addSubview(actInd)
        actInd.startAnimating()
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        actInd.center = self.view.center
    }
    
    /* Following two functions are acceptable also.
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        actInd.center = self.view.center
    }
    
    override func viewDidLayoutSubviews() { 
        super.viewDidLayoutSubviews()
        actInd.center = self.view.center
    }
    */