pulp
pulp

Reputation: 1828

UIActivityIndicatorView in the same hierarchy of UITableView in UITableViewController

I have a UIViewController and I dropped a UITableView and UIActivityIndicatorView in the same hierarchy and it's working fine.

But then I have a UITableViewController, with a UITableView of course, and I try to drop a UIActivityIndicatorView at the same hierarchy as the Table View but with no luck. I know that there's a problem with UITableViewController and UIViewController with Table View but how to solve?

These two screenshots will help to understand the problem.

The way I want it to be:

enter image description here

The way it turns out:

enter image description here

Upvotes: 0

Views: 44

Answers (3)

Developer Sheldon
Developer Sheldon

Reputation: 2160

When you drag and drop any view (no matter if it is UILabel or UIActivityIndicatorView or UIView),

if it is above the cell you put there, it will be treated as the TableViewHeader automatically.

if it is below the cell you put there, it will be treated as the TableViewFooter automatically.

That is the reason why they are at the same level of view hierarchy of cells.

If you really want to use the indicatorView at same level as your tableView in your tableViewController, even if you programmatically create it and add it to your tableviewcontroller's self.view, it WILL NOT work, because tableViewController's self.view is its content view, which is scrollable anyways, which is kinda a bummer. We only have full control in UIViewController.

Upvotes: 1

pulp
pulp

Reputation: 1828

Ok, guys I found one more solution.

Define a variable inside UITableViewController

weak var spinner: UIActivityIndicatorView!

then in viewDidLoad method make this (hope the code is self explanatory)

let activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .gray)
tableView.backgroundView = activityIndicatorView
tableView.separatorStyle = .none
self.spinner = activityIndicatorView

and then call spinner.startAnimating() when you need to show it, and

self.spinner.stopAnimating()
self.tableView.separatorStyle = .singleLine

to remove it when you don't need it.

Upvotes: 1

Shehata Gamal
Shehata Gamal

Reputation: 100503

The problem is that with the UIViewController it has a self.view as the root view where you can add any sub-items as you did , put you can't do this at least in IB with UITableViewController as the root view is the table itself

First way

add it in code and control is position as the tableView scrolls so change it's frame in scrollViewDidScroll so it's stay at center of screen during the loading

Second way

add it to the main window of the app and remove it when loading finishes

Upvotes: 1

Related Questions