用UIActivityIndi​​catorViewreplaceUIBarButtonItem

我想用UIActivityIndicatorViewreplace我的UIBarButtonItem (用于刷新),当刷新完成时,我想回到刷新button并删除UIActivityIndicatorView

只需创build两个不同的UIBarButtonItem

一个用于活动指示器,另一个用于正常的UIBarButtonItem。

 UIActivityIndicatorView * activityView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)]; [activityView sizeToFit]; [activityView setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin)]; UIBarButtonItem *loadingView = [[UIBarButtonItem alloc] initWithCustomView:activityView]; [self.navigationItem setRightBarButtonItem:loadingView]; [loadingView release]; [activityView release]; UIBarButtonItem * normalButton = [[UIBarButtonItem alloc] initWithTitle...]; [self.navigationItem setRightBarButtonItem:normalButton]; [normalButton release]; 

当你想切换它们时,只需重新分配rightBarButtonItem即可。

这里对我有用:

 - (void) rightItemButtonWithActivityIndicator { UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; [activityIndicator startAnimating]; UIBarButtonItem *activityItem = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator]; [activityIndicator release]; self.navigationItem.rightBarButtonItem = activityItem; [activityItem release]; } 

我试图做同样的事情,我想设置self.navigationItem.rightBarButtonItem不工作,因为活动指标不会显示。 原来,它工作正常,我只是不能看到它,因为我有一个白色的导航栏和默认的UIActivityIndi​​catorView风格也是白色的。 所以它在那里,但看不见 随着灰色的风格,我现在可以看到它。

  UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 

(杜)。

在重新加载UIWebView时,我使用了类似的技术来更新UIToolbar中的button(因为它看起来不可能显示/隐藏单独的条形button项目)。 在这种情况下,您需要换出UIToolbar中的所有项目。

 @property (strong, nonatomic) IBOutlet UIBarButtonItem *refreshBarButton; @property (nonatomic, strong) UIActivityIndicatorView *activityView; @property (nonatomic, strong) UIBarButtonItem *activityBarButton; @property (strong, nonatomic) IBOutlet UIToolbar *toolbar; @property (strong, nonatomic) IBOutlet UIBarButtonItem *backBarButton; @property (strong, nonatomic) IBOutlet UIBarButtonItem *refreshBarButton; @property (strong, nonatomic) IBOutlet UIBarButtonItem *forwardBarButton; 

 #pragma mark - UIWebViewDelegate -(void)webViewDidFinishLoad:(UIWebView *)webView{ [self updateButtons]; } -(void)webViewDidStartLoad:(UIWebView *)webView{ [self updateButtons]; } -(void)updateButtons{ /* It's not possible to show/hide bar button items so we need to do swap out the toolbar items in order to show the progress view */ //Initialise the activity view if (self.activityBarButton == nil){ self.activityView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; self.activityBarButton = [[UIBarButtonItem alloc] initWithCustomView:self.activityView]; self.activityBarButton.enabled = NO; } NSMutableArray *toolbarItems = [[NSMutableArray alloc] initWithArray:self.toolbar.items]; if ([self.webview isLoading]){ //Replace refresh button with loading spinner [toolbarItems replaceObjectAtIndex:[toolbarItems indexOfObject:self.refreshBarButton] withObject:self.activityBarButton]; //Animate the loading spinner [self.activityView startAnimating]; } else{ //Replace loading spinner with refresh button [toolbarItems replaceObjectAtIndex:[toolbarItems indexOfObject:self.activityBarButton] withObject:self.refreshBarButton]; [self.activityView stopAnimating]; } //Set the toolbar items [self.toolbar setItems:toolbarItems]; //Update other buttons self.backBarButton.enabled = [self.webview canGoBack]; self.forwardBarButton.enabled = [self.webview canGoForward]; }