简体   繁体   中英

How to make a UIActivityIndicatorView in a UIToolbar tappable

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?

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.

最后,我通过用actionRecognizer在activityIndi​​catorView之上添加了一个额外的视图,从而实现了一个穷人的解决方案。

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM