5

I have put activity indicator

 spinner.center = CGPointMake( [UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);
[self.view addSubview:spinner];

But this is not properly centered in the table view. Because table view can scroll. How can i put a spinner in the center of the screen.

Susitha
  • 3,339
  • 5
  • 27
  • 41
  • 1
    You are making it centered of screen not of `tableView`. It will contain the **height** of `navigaitonBar` and `statusBar` too. **table view can scroll** doesn't make any sense here. – TheTiger Aug 01 '13 at 04:55
  • For Swift 3 you can use the following: Please check my answer in http://stackoverflow.com/a/41748916/2307418 – Ahmed Lotfy Jan 22 '17 at 06:38

5 Answers5

9

try with bellow code:-

    spinner.center = CGPointMake( [UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);
    yourAppdelegateClass *appDelegate = (yourAppdelegateClass*)[[UIApplication sharedApplication] delegate];
    [appDelegate.window addSubview:spinner];

Code Output is:-

enter image description here

Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
  • UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; spinner.center = CGPointMake( [UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2); ; UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow]; [mainWindow addSubview: spinner]; This works for me. – Susitha Aug 01 '13 at 06:24
  • 1
    bravo.. this is same thing you UIWindow mainWindow = [[UIApplication sharedApplication] keyWindow]; and object of appdelegate and you get window as par my code... :) – Nitin Gohel Aug 01 '13 at 06:26
4

Try this:

spinner.center = tableview.center;

Hope it Helps!!

Armaan Stranger
  • 3,140
  • 1
  • 14
  • 24
  • 1
    It'd be much safer to use `CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds))` since `tableview`'s center is going to be relative to it's superview and not itself! – Warpling Jul 21 '15 at 04:02
1

I think you should add your indicater(Spinner) on table view instead of sel.view. and define the area frame on the indicater.

hope this will help you.

iOS Rocks
  • 176
  • 1
  • 8
1

I have another solution, because I have to put a spinner at detail view controller. Maybe it will help somebody.

- (void)viewDidLoad {
    [super viewDidLoad];
    self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.spinner.hidesWhenStopped = YES;
    [self.navigationController.view addSubview:self.spinner];
}

And for positioning:

-(void)viewDidLayoutSubviews
{  
    [super viewDidLayoutSubviews];
    [self.navigationController.view setNeedsLayout];
    [self.navigationController.view layoutIfNeeded];
    self.spinner.frame = CGRectMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2, 10, 10);
}

Spinner will be always at center of your table view at detail view controller.

Nikita
  • 317
  • 3
  • 10
-1

Please find below code:

UIActivityIndicatorView* spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.center = CGPointMake( [UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[spinner startAnimating];
[appDelegate.window addSubview:spinner];
Priyank Gandhi
  • 1,257
  • 1
  • 16
  • 29
Sujatha C
  • 1
  • 1