How to set the activity indicator in the navigation bar?

32,557

Solution 1

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

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

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 4

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 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)
    }
    
Share:
32,557
Warrior
Author by

Warrior

I am a software engineer.I have to learn lot in this field.

Updated on July 03, 2021

Comments

  • Warrior
    Warrior almost 3 years

    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.

  • Warrior
    Warrior over 14 years
    I want to display the activity indicator in the navigation bar
  • antf
    antf over 10 years
    I noticed that on iOS 7 you need to use activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleG‌​ray]; or the indicator will not appear.
  • Andrew Duncan
    Andrew Duncan over 9 years
    I needed Warrior's solution because I am hiding the status bar. Not that there's anything wrong with your solution of course!
  • Nurbol
    Nurbol over 9 years
    This goes into the status bar though
  • djbp
    djbp over 9 years
    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.
  • Michael Garito
    Michael Garito over 7 years
    This crashes in 10.1.1 with: libc++abi.dylib: terminating with uncaught exception of type NSException
  • evya
    evya over 6 years
    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.
  • Baran Emre
    Baran Emre almost 6 years
    Works on 11.4 without any issues.