How to set the activity indicator in the navigation bar?

IphoneFrameActivity IndicatorNavigationbar

Iphone Problem Overview


I am new to iphone development. I want to set an activity indicator in the navigation bar. I see my activity indicator below the navigation bar. My code is here

- (IBAction) gomethod : (id) sender {
    xxMapSubviewcontroller = [[XxMapSubviewcontroller alloc] init];
	[self.navigationController pushViewController:xxMapSubviewcontroller animated:YES];

    activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
	activityIndicator.frame = CGRectMake(0.0, 0.0, 20.0, 20.0);
    [activityIndicator startAnimating];

	[xxMapSubviewcontroller.view addSubview:activityIndicator];
}

How can i set my activity indicator in the navigation bar? Please help me out. Thanks.

Iphone Solutions


Solution 1 - Iphone

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

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

Solution 2 - Iphone

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

Solution 3 - Iphone

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

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

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

Solution 4 - Iphone

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

Solution 5 - Iphone

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

Solution 6 - Iphone

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

Solution 7 - Iphone

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
}

Solution 8 - Iphone

Thanks! My indicator is working now.

I am sharing a code example for fellow noobs, to 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];
  }

Solution 9 - Iphone

In Swift, I did the following:

Enable: UIApplication.sharedApplication().networkActivityIndicatorVisible = true

Disable: UIApplication.sharedApplication().networkActivityIndicatorVisible = false

Solution 10 - Iphone

I had a similar question with response here: https://stackoverflow.com/questions/2249363/iphone-programatically-change-navigation-bar-button-to-activity-indicator

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

Solution 11 - Iphone

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

Solution 12 - Iphone

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

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

With this change it should work as expected.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWarriorView Question on Stackoverflow
Solution 1 - IphoneWarriorView Answer on Stackoverflow
Solution 2 - IphoneMkeyView Answer on Stackoverflow
Solution 3 - IphoneZoran SimicView Answer on Stackoverflow
Solution 4 - IphoneAxeView Answer on Stackoverflow
Solution 5 - IphoneHamedView Answer on Stackoverflow
Solution 6 - IphoneevyaView Answer on Stackoverflow
Solution 7 - IphoneUrvish ModiView Answer on Stackoverflow
Solution 8 - IphoneAnthony De SouzaView Answer on Stackoverflow
Solution 9 - IphonedjbpView Answer on Stackoverflow
Solution 10 - IphonecagreenView Answer on Stackoverflow
Solution 11 - IphoneiXcoderView Answer on Stackoverflow
Solution 12 - IphoneNirView Answer on Stackoverflow