1

Can we add activity indicator as subview to UIButton?

If yes then plz tell me how to do that?

I used [button addSubview:activityIndicator];

Not working...

Ratan
  • 1,747
  • 2
  • 18
  • 27
  • What's not working? Please include any error messages you get and also how these objects are declared and instantiated. Also, in what method are you invoking addSubview:? – Dave DeLong Sep 08 '10 at 13:44
  • Code is correct. Please show more code, i.e. how do you setup the indicator? – Eiko Sep 08 '10 at 13:45
  • Dup of http://stackoverflow.com/questions/2469184/how-to-set-an-activity-indicator-on-custom-button-in-iphone – ennuikiller Sep 08 '10 at 13:45
  • I have added the 'button' as subview in a table view cell. And I wanted to show an activity indicator above that button which I have to do in some other method. I have declared the button in .h file. – Ratan Sep 08 '10 at 13:50
  • I just want to show activity indicator in place of the button in the cell of the table view... – Ratan Sep 08 '10 at 13:59

4 Answers4

2

I found that adding a UIActivityIndicatorView to a UIButton was a really useful method to allow users to know something is happening without having to use the MBProgressHUD (I think the HUD is really good but should not be used in all situations.

For this reason I created two functions:

I have already allocated my UIButton so it is a class variable called _confirmChangesButton I then create my activity indicator, set its frame (taking into account the button size) and then adding the indicator is easy.

- (void)addActivityIndicatorToConfirmButton {

    // Indicator needs to be in the middle of the button. So half the screen less half the buttons left inset less half the activity indicator size
    CGRect rect = CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 10 - 15, 5, 30, 30);

    UIActivityIndicatorView * activity = [[UIActivityIndicatorView alloc] initWithFrame:rect];
    activity.hidesWhenStopped = YES;

    [_confirmChangesButton setTitle:@"" forState:UIControlStateNormal];
    [_confirmChangesButton addSubview:activity];
    [activity startAnimating];
}

Having a removal function is also useful if you are using blocks. It might be that the completion task comes back with a failure and so we want to remove the indicator and change the title back. In this function we need to make sure to remove the indicator and not the button label which is the other subview on this button.

- (void)removeActivityIndicatorFromConfirmButton {

    UIActivityIndicatorView * activity = _confirmChangesButton.subviews.lastObject;;

    [activity removeFromSuperview];

    [_confirmChangesButton setTitle:@"Confirm Change" forState:UIControlStateNormal];
}

I found that using these two you can create a much better user experience letting the user know what is going on when they press buttons.

Hope this helps

sam_smith
  • 6,023
  • 3
  • 43
  • 60
  • 1
    Yes true. This adds to nice user experience. I'll accept this as an ans so that any user looking for this query can get the ans. – Ratan Dec 17 '14 at 05:28
1

Use the below code below to add acitivity indicator a button or any uiview object

UIActivityIndicatorView *aView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
aView.frame = CGRectMake(0, 0, {yourButton}.frame.size.width, {yourButton}.frame.size.height);
aView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
[{yourButton} addSubview:aView];
[aView startAnimating];

Hope this will help..

Avinash
  • 4,304
  • 1
  • 23
  • 18
0

I don't think it's possible to add a view to a button. UIButton have this method because it's inherited from UIVIew. The real question is : why do you want to add an activity indicator on a button and not elsewhere ?

  • Whether it was possible in 2010 I don't know but it is certainly possible now and is very useful. Adding an activity indicator to a uibutton lets a user know the button has been pressed even if there is a slight delay before something happens. – sam_smith Dec 16 '14 at 12:12
0

did you do [activityIndicator startAnimating]; ALso as u are using it in a tableview just check if the tags are set properly

B K
  • 1,554
  • 3
  • 19
  • 40