[ACCEPTED]-How to set the activity indicator in the navigation bar?-navigationbar

Accepted answer
Score: 87

I add the below piece of code in the view 2 where i wanted the activity indicator in 1 the navigation bar.

activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
[self navigationItem].rightBarButtonItem = barButton;
[activityIndicator startAnimating];
Score: 27

Swift Code:

let activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
let barButton = UIBarButtonItem(customView: activityIndicator)
self.navigationItem.setRightBarButton(barButton, animated: true)
activityIndicator.startAnimating()

0

Score: 13

You are creating a new activity indicator 4 view here, which is fine, but you are not 3 referring to the activity indicator in the 2 status bar.

To show the activity indicator 1 in the status bar, simply call this:

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
Score: 13

This worked for me in Swift:

let activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView.init(activityIndicatorStyle: .White)
let refreshBarButton: UIBarButtonItem = UIBarButtonItem(customView: activityIndicator)
self.navigationItem.leftBarButtonItem = refreshBarButton
activityIndicator.startAnimating()

0

Score: 5

On storyboard: Create BarButtonItem at the navigation bar. Add View 2 to be son of your BarButtonItem and ActivityIndicator to be son of your 1 View.

Score: 5

Swift

  1. Connect UIBarButtonItem from storyboard to yourViewController
  2. remove week from its definition like: @IBOutlet var btNavigaitonRight: UIBarButtonItem!
  3. Use these methods for start and stopping 1 activity indicator:

    var activityIndicator = UIActivityIndicatorView()
    
    func startBarButtonIndicator() {
        activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
        activityIndicator?.color = .gray
        let barButton = UIBarButtonItem(customView: activityIndicator!)
        self.navigationItem.setRightBarButton(barButton, animated: true)
        activityIndicator?.startAnimating()
    }
    
    func stopBarButtonIndicator() {
        activityIndicator?.stopAnimating()
        navigationItem.setRightBarButton(btNavigaitonRight, animated: true)
    }
    
Score: 3

Like WhatsApp:

//Declare

let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)

//ViewDidLoad

self.activityIndicator.hidesWhenStopped = true

func showIndicator() {
    self.navigationItem.titleView = self.activityIndicator
    self.activityIndicator.isHidden = false
}

func hideIndicator() {
    self.navigationItem.titleView = nil
}

0

Score: 2

Thanks! My indicator is working now.

I am 2 sharing a code example for fellow noobs, to 1 put this in context.

- (void)viewDidLoad

// custom button images
UIImage *customImage = [UIImage imageNamed:@"menu24"];
UIImage *customImage2 = [UIImage imageNamed:@"search24"];
UIImage *customImage3 = [UIImage imageNamed:@"back24"];

// These are linked in my story board to Navigation Item
[self customiseBarBtnItem:[self menu_button] 
    customImage:customImage selector:@selector(menuPressed:)];
[self customiseBarBtnItem:[self search_button] 
    customImage:customImage2 selector:@selector(searchPressed:)];
[self customiseBarBtnItem:[self backButton] 
    customImage:customImage3 selector:@selector(backPressed:)];

//initialize the activity indicator - as @antf comment suggests for ios7   
UIActivityIndicatorView *actInd=[[UIActivityIndicatorView alloc]                                      
     initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

//store it as a property on the view controller
self.activityIndicator = actInd;

// this sets up activity indicator    
UIBarButtonItem *progress_indicator = [[UIBarButtonItem alloc] 
     initWithCustomView:[self activityIndicator]];

// link custom buttons AND activity indicator in desired order to navigation bar
self.navigationItem.rightBarButtonItems =
    [NSArray arrayWithObjects:
         self.menu_button,
         self.search_button, 
         progress_indicator, 
         nil];

// For completeness - this is how I programmatically show/hide my back button

if ( bShowBack == YES ) 
     self.navItemBar.leftBarButtonItem = self.backButton;
else 
     self.navItemBar.leftBarButtonItem = Nil;

// I use my activity indicator with a UIWebView so trigger it as follows

  - (void)webViewDidStartLoad:(UIWebView *)webView
  {    
    [[self activityIndicator] startAnimating];
  }

  - (void)didFailLoadWithError:(UIWebView *)webView 
          didFailLoadWithError:(NSError *)error
  {    
     [[self activityIndicator] stopAnimating];
  }

  - (void)webViewDidFinishLoad:(UIWebView *) webView
  {
     [[self activityIndicator] stopAnimating];
  }
Score: 2

In Swift, I did the following:

Enable: UIApplication.sharedApplication().networkActivityIndicatorVisible = true

Disable: UIApplication.sharedApplication().networkActivityIndicatorVisible = false

0

Score: 1

I had a similar question with response here: iphone - programatically change navigation bar button to activity indicator

I 3 wanted to change the refresh button in the 2 nav bar to the activity indicator and back 1 again.

Score: 1

try this : self.navigationItem.leftBarButtonItem.customView = your_view

0

Score: 0

I tried to use it now and the code mentioned 3 by warrior didn't work exactly as is. I 2 had to change the initialization of activityIndicator:

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

With 1 this change it should work as expected.

More Related questions