Reputation: 3600
I have a table view controller and I display a UIActivityIndicatorView in the center of the screen in viewDidLoad()
let bounds = UIScreen.mainScreen().bounds;
let width = bounds.size.width
let height = bounds.size.height
loadingIndicator = UIActivityIndicatorView(frame: CGRectMake(width / 2, height / 2, 75, 75))
loadingIndicator.layer.cornerRadius = 10
loadingIndicator.backgroundColor = kfbBlue
loadingIndicator.center = CGPointMake(width / 2, height / 2 - 37)
loadingIndicator.activityIndicatorViewStyle = .WhiteLarge
loadingIndicator.hidesWhenStopped = true
self.tableView.addSubview(loadingIndicator)
loadingIndicator.startAnimating()
This works just fine. I have a reload method for reloading the table view data. reload() works, but if I press the button that calls this method after I have scrolled the table view, the activity indicator does not appear. It only appears if the table view is scrolled up to the top.
func reload() {
loadingIndicator.startAnimating()
self.beginParsing()
}
How can I get the activity indicator to appear in the center regardless of how far the table view has been scrolled?
Upvotes: 1
Views: 526
Reputation: 13650
Don't add the UIActivityIndicatorView
to the UITableView
. Add it to the center pf the parent view that holds the UITableView
. You are adding it to the UITableView
's content view, which will scroll. Add it outside that if you want it to persist even when the UITableView
has been scrolled. Most likely something like this:
self.view.addSubview(loadingIndicator)
instead of your line
self.tableView.addSubview(loadingIndicator)
If you are using a UITableViewController
, the topmost view will be the TableView, so you that won't work. You will instead have to adjust the center of the activity indicator to take into account the scroll location:
loadingIndicator.center = CGPointMake(width / 2, self.tableView.contentOffset.y + height / 2 - 37)
Upvotes: 1