Adding a UIActivityIndicator to a UIToolbar

When building apps that fetch data from the network you may want to add an activity indicator to your application’s toolbar. Unfortunately, there is no built in activity indicator bar item. Luckily, it’s easy to create one in code.

The key is to set the activity indicator as the custom view in a UIBarButtonItem:

    self.loadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    self.activityBarItem.customView = self.loadingIndicator;

That’s all there is to it. You can create the bar item in IB and then create the indicator in the controller. Once you’ve created the indicator, just assign it to the customView property of the UIBarButtonItem. You’ll want to set the width of the UIBarButtonItem to 20px in IB.