2

I'm implementing some sort of loading screen consisting of a black coloured uiview with a spinner at it's center.

When the tableview has been scrolled down and the loading goes, the loading screen is at the top and if the tableview has been scrolled to the bottom, the loading screen disappears.

Function:

UIView *blackScreen = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
blackScreen.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
blackScreen.tag = LOADING_SCREEN_TAG;

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

spinner.center = CGPointMake(CGRectGetWidth(self.view.bounds)/2,
                             CGRectGetHeight(self.view.bounds)/2.3);

spinner.tag = SPINNER_TAG;

[spinner startAnimating];

[self.view addSubview:blackScreen];
[self.view addSubview:spinner];

[self.view setUserInteractionEnabled:NO];
rmaddy
  • 314,917
  • 42
  • 532
  • 579

2 Answers2

0

You need to add contentOffset, I would like to refer you to this page which explains the use of it.

Here's the solution anyway, this is assuming that the variable name of your tableview is tableView and is set as a property of the class:

CGRect loadingScreenFrame = CGRectMake([[UIScreen mainScreen] bounds].origin.x + self.tableView.contentOffset.x,
                                       [[UIScreen mainScreen] bounds].origin.y + self.tableView.contentOffset.y,
                                       [[UIScreen mainScreen] bounds].size.width,
                                       [[UIScreen mainScreen] bounds].size.height);

UIView *blackScreen = [[UIView alloc] initWithFrame:loadingScreenFrame];
blackScreen.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
blackScreen.tag = LOADING_SCREEN_TAG;

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

spinner.center = CGPointMake((CGRectGetWidth(self.view.bounds)/2) + self.tableView.contentOffset.x,
                             (CGRectGetHeight(self.view.bounds)/2.3) + self.tableView.contentOffset.y);

spinner.tag = SPINNER_TAG;

[spinner startAnimating];

[self.view addSubview:blackScreen];
[self.view addSubview:spinner];

[self.view setUserInteractionEnabled:NO];
Community
  • 1
  • 1
Bryan P
  • 4,142
  • 5
  • 41
  • 60
  • 1
    If it answers your question, please click the check button beside my answer so as a reference for future visitors – Bryan P May 16 '14 at 02:27
0

A solution would be to not add the spinning view to the tableview but rather to its superview. It seems like you are probably using a UITableViewController which would mean that you would need to restructure your code to use a UIViewController to make this work. Adding both the UITableView and the spinner view to the view of the UIViewController would also achieve what you are looking for without having to set the center based on the content offset.

Pete42
  • 916
  • 7
  • 15