views:

739

answers:

2

My application connects to the network to retrieve some data and I'd like to show a progress bar in the toolbar of the UINavigationController of my application.

What I actually want is very similar to the Mail application:

                                      Screenshot of Mail application showing an example toolbar layout

Except I would like to have nothing to the left of the progress bar, and a cancel button on the right.

I've fiddled around with code, trying to do this, but I've never worked with the toolbar of a nav controller before, so I'm unsure of where to start.

Does anyone know I might go about doing this, or where I could find resources explaining how to do this?

I've read over the Human Interface Guide, the UINavigationController class reference, and the View Controller Programming Guide, but they only show how to do very basic toolbar layouts, with simple buttons and segmented controls.

+1  A: 

This is quite easy using IB. Just drag in a Toolbar to your view, then drag an NSProgressIndicator. Finally, drag an NSFlexibleSpacer in to the left of the progress bar, and your cancel button to the right. You may need to add a few NSSpacers to the left and right to get everything to line up properly.

Ben Gottlieb
Can I still use IB even though I want to use the UINavigationController's toolbar? I don't want to create another toolbar in a seperate xib.
Ben S
Also, does the progress bar have an associated label or do I have to add my own? The class reference doesn't have any mention of a label, but the toolbar has an `initWithTitle` initializer.
Ben S
Darren's comment below shows the way to do this programatically. You want to use an custom UIBarButtonItem, and its initWithCustomView.
Ben Gottlieb
+2  A: 

Start with an empty UIView, add a text label and a progress indicator and position them as they appear in the Mail app. Finally, create a UIBarButtonItem that contains this view and add it to your toolbar:

UIBarButtonItem* progressItem = [[UIBarButtonItem alloc] initWithCustomView:progressItemView];
toolbar.items = [NSArray arrayWithObject:progressItem];

You can do this programmatically or in Interface Builder.

Darren
How could I get the label above the progress view? So far I have a Flex space to the left, the progress view and a cancel button to the right, but I'm not sure how to go about getting text above the progress view. I've tried setting the UIBarButtonItem title property, but that doesn't display anywhere.
Ben S
Put an empty UIView in your toolbar. Add a UILabel and a UIProgressIndicator to that view and position them accordingly. You can do that all within Interface Builder, or you can design your view in Interface Builder and then add that view to your toolbar programmatically.
Darren