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
- Connect
UIBarButtonItem
fromstoryboard
toyourViewController
- remove
week
from its definition like:@IBOutlet var btNavigaitonRight: UIBarButtonItem!
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) }
Author by
Warrior
I am a software engineer.I have to learn lot in this field.
Updated on July 03, 2021Comments
-
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 over 14 yearsI want to display the activity indicator in the navigation bar
-
antf over 10 yearsI noticed that on iOS 7 you need to use
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
or the indicator will not appear. -
Andrew Duncan over 9 yearsI needed Warrior's solution because I am hiding the status bar. Not that there's anything wrong with your solution of course!
-
Nurbol over 9 yearsThis goes into the status bar though
-
djbp over 9 yearsYou'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 over 7 yearsThis crashes in 10.1.1 with: libc++abi.dylib: terminating with uncaught exception of type NSException
-
evya over 6 yearsYes, 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 almost 6 yearsWorks on 11.4 without any issues.