0

While running an application in iOS, when we click on any button (UIButton), a default loading spinner spins in the center of the screen till the action is performed. However, I dont want that loading spinner to pop up, instead I want a custom indicator to spin at another position. I am using UIActivityIndicator here and the custom indicator is working fine at its position. But the default spinner in the center is still coming along on the click. Please suggest the correct way so that the default loading spinner is hidden or does not pop up on the button click.

UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] 
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicator.frame= CGRectMake(textView.frame.size.width+30, 
containerView.frame.size.height-45,40,40);
indicator.autoresizingMask=UIViewAutoresizingFlexibleTopMargin | 
UIViewAutoresizingFlexibleRightMargin;
[containerView addSubview:indicator];
indicator.backgroundColor = [UIColor clearColor];
[indicator startAnimating];
.
.
.

After button action performed..

[indicator stopAnimating];

2 Answers2

0

Try this :

UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] 
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicator.frame= CGRectMake(textView.frame.size.width+30, 
containerView.frame.size.height-45,40,40);
indicator.autoresizingMask=UIViewAutoresizingFlexibleTopMargin | 
UIViewAutoresizingFlexibleRightMargin;
[indicator setHidesWhenStopped:YES]; // i added this 
[containerView addSubview:indicator];
indicator.backgroundColor = [UIColor clearColor];
[indicator startAnimating];
KKRocks
  • 8,222
  • 1
  • 18
  • 84
0

Your code keeps on adding a NEW UIActivityIndicatorView everytime you call this code. Eventually you will have 100s UIActivityIndicator that are hidden in your view. What you should do is declare indicator as property, and create it only if it is nil.

@property (nonatomic, strong) UIActivityIndicatorView *indicator;

-(void)showProcessing {
    if (_indicator==nil)  // here checks for existing indicator in memory
      _indicator = [[UIActivityIndicatorView alloc]  initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    indicator.frame= CGRectMake(textView.frame.size.width+30, 
    containerView.frame.size.height-45,40,40);
    indicator.autoresizingMask=UIViewAutoresizingFlexibleTopMargin | 
    UIViewAutoresizingFlexibleRightMargin;
    [_indicator setHidesWhenStopped:YES];
    [containerView addSubview:_indicator];
    indicator.backgroundColor = [UIColor clearColor];
    [indicator startAnimating];
}

And hiding:

-(void)hideProcessing {
  [_indicator stopAnimating];
}
GeneCode
  • 7,545
  • 8
  • 50
  • 85