简体   繁体   English

如何在可点击的UIToolbar中制作UIActivityIndi​​catorView

[英]How to make a UIActivityIndicatorView in a UIToolbar tappable

I'm using this code to insert an UIActivityIndicatorView to my toolbar 我正在使用此代码将UIActivityIndi​​catorView插入工具栏

-(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. 但是,当我点击UIActivityIndi​​catorView时,未执行操作(playButtonPressed)。

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 ? 这是一个很老的问题,但是为什么不直接将UITapGestureRecognizer实例添加到UIActivityIndicatorView实例呢? (works fine on iOS 8.2, I didn't test yet on previous versions). (在iOS 8.2上运行良好,我尚未在以前的版本上进行测试)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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