2010-06-03 82 views

回答

48

只需创建两个不同的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取其。

+0

我有另一个小问题。有了这个实现我怎么能在UIActivityIndi​​catorView上调用beginAnimation和stopAnimation?谢谢 – Luca 2010-06-03 12:56:05

+0

你可以为UIActivityIndi​​catorView创建一个属性。 – 2010-06-03 12:59:29

+0

非常感谢 – Luca 2010-06-03 15:20:55

10

这里什么对我的作品:

- (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]; 
} 
2

我使用了类似的技术,一个UIToolbar更新按钮时,一个UIWebView重新加载(因为它似乎没有可以显示/隐藏个人吧按钮项目)。在这种情况下,您需要换出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]; 
} 
7

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

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

(杜)

相关问题