UIProgressBar: When Spinny Things Are Tacky

The UIProgressBar object is a close cousin to the UIProgressIndicator. Instead of displaying a drool-inciting animation, the progress bar class draws a thermometer-like indicator and provides an interface to set its fill level as your application crunches on its operation. The advantage of using a progress bar is that it can reflect more or less accurately how much work the application has actually done.

To create a progress bar, the class's initialization method includes a frame identifying the bar's size and display origin:

UIProgressBar progressView = [ [ UIProgressBar alloc ]
    initWithFrame: CGRectMake(0, 0, 320, 32) ];

The indicator supports one of two styles, white (0) and dark gray (1), which can be assigned using the setStyle method, according to the color of the object it's being attached to.

[ progressView setStyle: 0 ];

To display the progress bar, add it to an existing UIView object. Code to attach it to an alert sheet might look like:

[ alertSheet addSubview: progressView ];

Once the progress bar has been displayed, its progress can be set by the application to indicate how far along it is in its operation. After the level is set, a call to the bar's updateIfNecessary method is made to ensure it is propagated out to the display. The progress value is a double (a floating point with double precision) between 0.0 and 1.0.

[ progressView setProgress: 0.5 ];

[ progressView updateIfNecessary ];

Example: A Better Built Bar

In this example, ...

Get iPhone Open Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.