views:

391

answers:

3

Hi,

I have a UITableViewController view a UITableView that I alloc/init with a frame of CGRectZero :

self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];

I want to add a view at the middle of the tableView (a loading view with a UIActivityIndicatorView and a UILabel), but I don't know how to position it, since I don't know the tableView frame.

x = (self.tableview.frame.size.width - loadingView.frame.size.width) / 2.0;
y = (self.tableview.frame.size.height - loadingView.frame.size.height) / 2.0;
[loadingView setCenter:CGPointMake(x, y)];

I did init my tableView with CGRectZero frame so it can take the whole place available on screen (which it did), but I thought its frame would update or something.

Any ideas ? Thanks !

A: 

I managed to estimate to frame programatically. I didn't mentionned that my tableView was between a navigationBar and a tabBar, so here is the code for this case :

CGFloat x = self.navigationController.view.frame.size.width;
CGFloat y = self.navigationController.view.frame.size.height -
            self.tabBarController.tabBar.frame.size.height;

self.tableView = [[UITableViewController alloc] initWithFrame:CGRectMake(0.0, 0.0, x, y)];

And the code posted above for the positionning of the loadingView works like a charm !

I'm open ton better solutions, of course

Thomas Joulin
A: 

If your View Controller is a UITableViewController then the frame of the table view is the same as the frame of the UIViewController.

Why don't you put it in the center of the UIViewController's view?

Assuming self is the UITableViewController:

loadingView.center = self.view.center;
progrmr
+1  A: 

You could give your LoadingView a reference to the view it is to appear over. Then when it is about to be shown, it can check the frame of the view it is representing to center and match that view and add itself as a subview of the TableView's superview:

- (void)show
{
    self.center = targetView.center;
    [[targetView superview] addSubview:self];
}

Another option is to have the LoadingView use KVO to monitor the UITableView's frame and adjust it's own frame accordingly.

John Haney