Rohit Dulam
Rohit Dulam

Reputation: 105

Not able to display an activity indicator on top of a static tableview

I'm not able to display an activity indicator on top a static table view though I wrote the below code in viewDidLoad.

let activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
tableView.backgroundView = activityIndicatorView
tableView.separatorStyle = UITableViewCellSeparatorStyle.none
self.activityIndicatorView = activityIndicatorView
self.activityIndicatorView.isHidden = false
self.tableView.addSubview(activityIndicatorView)
self.activityIndicatorView.startAnimating()

Upvotes: 2

Views: 1035

Answers (3)

Vandana pansuria
Vandana pansuria

Reputation: 764

If you use pull to refresh then implement this code:

  var refreshControl: UIRefreshControl!

override func viewDidLoad() {
   super.viewDidLoad()

   refreshControl = UIRefreshControl()

   refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
   tableView.addSubview(refreshControl) 
}

func refresh(sender:AnyObject) {
   tableView.reloadData()
   refreshControl.endRefreshing()  
}

Upvotes: 0

Nirav D
Nirav D

Reputation: 72420

Instead of adding UIActivityIndicatorView in tableView, add it to inside your main view and call the bringSubview(toFront:) on the main view to to bring the indicatorView to front and you have't set the origin position for your indicatorView set that also.

self.activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
tableView.separatorStyle = UITableViewCellSeparatorStyle.none
self.activityIndicatorView.isHidden = false

//Add inside your view
self.view.addSubview(activityIndicatorView)

//Set activityIndicatorView origin in the screen
self.activityIndicatorView.center = self.view.center

//Try to bring activityIndicatorView to front
self.view.bringSubview(toFront: activityIndicatorView)

self.activityIndicatorView.startAnimating()

Upvotes: 3

PlusInfosys
PlusInfosys

Reputation: 3446

You cant add subview to tableview, Try adding it on Navigation controllers view instead.

like :

self.view.superviewaddSubview(activityIndicatorView);

Upvotes: 0

Related Questions