0

I'm using this code to insert an UIActivityIndicatorView to my toolbar

-(void)addActivityIndicatorToToolbar {
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    activityIndicator.userInteractionEnabled = YES;
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    [activityIndicator startAnimating];

    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
    barButton.target = self;
    barButton.action = @selector(playButtonPressed:);

    NSMutableArray *toolbarItemsMutable = [self.toolbar.items mutableCopy];
    [toolbarItemsMutable replaceObjectAtIndex:0 withObject:barButton];
    self.toolbar.items = toolbarItemsMutable;  
}

However, when I tap the UIActivityIndicatorView the action (playButtonPressed) is not performed.

How can I correct this?

murze
  • 4,015
  • 8
  • 43
  • 70

3 Answers3

1

It seems more likely that you want a button with an activity indicator inside it. You can do this by creating a button with a custom view as described in this post. Then you can set the action of this button as normal, and you'll probably want to retain a reference to the activity indicator to start and stop it.

Community
  • 1
  • 1
Vic Smith
  • 3,477
  • 1
  • 18
  • 29
  • I used the storyboard to add the toolbar to my view. It does not have the property rightBarButtonItem... – murze Aug 31 '12 at 11:59
  • On reading your post more clearly I can see you are setting the activity indicator as a custom view inside a button, apologies for misreading. Does the button show the activity indicator and push in when you press it? It could be the selector name. Is the method `-(void)playButtonPressed:(id)sender` defined in the same class, or do you have `-(void)playButtonPressed`? – Vic Smith Aug 31 '12 at 14:35
  • the button does not push it. i've got -(void)playButtonPressed:(id)sender defined in the same class (so the colon on the end of the selector is correct) Meanwhile I implemented a poor man's solution by adding an extra view on top of the activityIndicatorView with a gestureRecognizer. A more elegant solution would be nice. – murze Aug 31 '12 at 14:48
  • Try setting `activityIndicator.userInteractionEnabled = NO` instead of `YES`. It could be that the activity indicator is eating the taps. – Vic Smith Aug 31 '12 at 14:56
0

I ended up implementing a poor man's solution by adding an extra view on top of the activityIndicatorView with a gestureRecognizer.

murze
  • 4,015
  • 8
  • 43
  • 70
0

This is a quite old question but why don't you directly add a UITapGestureRecognizer instance to your UIActivityIndicatorView instance ? (works fine on iOS 8.2, I didn't test yet on previous versions).

yonel
  • 7,855
  • 2
  • 44
  • 51