Douglas
Douglas

Reputation: 2524

UIActivityIndicatorView in a UITableViewController

I am currently updating one of my apps. It fetches information from the internet and uses networkActivityIndicatorVisible to show and hide the Activity Indicator in the status bar. The one complaint we have received about this, is that it is too small, and users don't know that their device is getting information. So my idea was to utilize the spinning activity wheel in the middle of my view. If you try to drag one out of the items, it places it in a cell, or in the header UIView that I made. This is not what I wanted so I made it programmatically.

In the header file.

@property (strong, nonatomic) UIActivityIndicatorView *mySpinner;

In the implementation file.

_mySpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
_mySpinner.color = [UIColor blackColor];
_mySpinner.translatesAutoresizingMaskIntoConstraints = NO;
_mySpinner.hidesWhenStopped = YES;

I then added the spinning wheel to my view by doing the following. Along with making my constraints.

[self.view addSubview:_mySpinner];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_mySpinner
                                                      attribute:NSLayoutAttributeCenterX
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeCenterX
                                                     multiplier:1
                                                       constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_mySpinner
                                                      attribute:NSLayoutAttributeCenterY
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeCenterY
                                                     multiplier:0.75
                                                       constant:0]];
[_mySpinner startAnimating];

I ran the code and it worked fine. Even when I rotated the spinner was where I wanted it. However, and this is where the problem lies, when I scroll the table view, the spinner goes right along with it!

I know this is due to the fact that I added the spinner to self.view and made all the constraints with self.view. And in the case of the UITableViewController, the table view is self.view.

The App has several tableviews and they are all quite long, many with custom cells. I don't really want to redesign everything, but does anyone know if there is a superview to the UITableViewController that I could put my constraints onto? I have tried a couple of things but nothing is working. Sorry for such a long question but I was hoping someone could help out. Thanks for reading!

Upvotes: 1

Views: 1584

Answers (1)

sanginadham murali
sanginadham murali

Reputation: 266

solution is just access the window of your application then attach the activity indicator for it like

 UIWindow *frontWindow = [[UIApplication sharedApplication] keyWindow];
    self.activity.center=frontWindow.center;

    [frontWindow addSubview:self.activity];
    [self.activity startAnimating];

link to code https://github.com/murali9/ActvityonTableview

Upvotes: 6

Related Questions