How to set the activity indicator in the navigation bar?
Asked Answered
M

12

54

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.

Marta answered 18/2, 2010 at 14:33 Comment(0)
M
88

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];
Marta answered 18/2, 2010 at 19:1 Comment(1)
I noticed that on iOS 7 you need to use activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; or the indicator will not appear.Diestock
R
30

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()
Round answered 12/4, 2016 at 22:31 Comment(0)
I
14

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()
Instable answered 15/7, 2016 at 21:54 Comment(2)
This crashes in 10.1.1 with: libc++abi.dylib: terminating with uncaught exception of type NSExceptionPedicle
Works on 11.4 without any issues.Coign
V
13

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];
Vitrics answered 18/2, 2010 at 14:59 Comment(2)
I want to display the activity indicator in the navigation barMarta
I needed Warrior's solution because I am hiding the status bar. Not that there's anything wrong with your solution of course!Totem
F
5

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

Floatation answered 15/12, 2014 at 12:51 Comment(1)
Yes, on StoryBoard: drag to navigation bar the BarButtonItem, now drag the UIView to be subview of BarButtonItem, now drag the ActivityIndicator to be subview of the UIView.Floatation
B
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 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)
    }
    
Bumboat answered 17/7, 2018 at 10:54 Comment(0)
B
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
}
Ballplayer answered 21/11, 2017 at 8:10 Comment(0)
B
2

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];
  }
Beaker answered 10/7, 2014 at 11:1 Comment(0)
R
2

In Swift, I did the following:

Enable: UIApplication.sharedApplication().networkActivityIndicatorVisible = true

Disable: UIApplication.sharedApplication().networkActivityIndicatorVisible = false

Reardon answered 7/1, 2015 at 13:8 Comment(2)
This goes into the status bar thoughLamarckism
You're not wrong. I mis-read the OP - apologies. Having said that, I came across this post searching for an answer of how to solve the problem of showing the network activity indicator in the status bar - hopefully my answer will still provide some help to someone, if not to the OP.Reardon
C
1

I had a similar question with response here: 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.

Coumas answered 18/2, 2010 at 19:51 Comment(0)
F
1

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

Flowerless answered 19/2, 2010 at 7:37 Comment(0)
P
0

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.

Pavkovic answered 1/1, 2014 at 21:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.