UINavigationController Toolbar - Add Status Text Using UIActivityIndicatorView

I would like to add a download activity indicator to my application, similar to the one located in the mail application with the status text on the right. I use UINavigationController, so I know that I need to set the toolbarItems array for each view where I want it to be displayed. I can add an activity indicator, and it appears, but when I try to add a text box using the code below, the text does not appear. Is there a way to create a container programmatically that has both status text and a UIActivityIndicatorView that will be displayed if it is added to the toolbarItems array.

UIBarButtonItem *textFieldItem = [[[UIBarButtonItem alloc] initWithCustomView:textField] autorelease];
self.toolbarItems = [NSArray arrayWithObject:textFieldItem];

UPDATE: I created a class derived from UIView based on the code from pdriegen.
I also added this code to view DidLoad in my controller

UIProgressViewWithLabel * pv = [[UIProgressViewWithLabel alloc] init];

UIBarButtonItem * pvItem = [[UIBarButtonItem alloc] initWithCustomView:pv];

[self setToolbarItems:[NSMutableArray arrayWithObject:pvItem]];

Nothing is displayed in the toolbar. What am I missing?

+3
source share
1 answer

Instead of adding an activity indicator and a label as separate views, create one composite view containing both of them, and add this composite view to the toolbar.

Create a class derived from UIView, override initWithFrame and add this code:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self configureView];
    }
    return self;
}

-(void)configureView{

    self.backgroundColor = [UIColor clearColor];

    UIActivityIndicatorView* activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];        
    activityIndicator.frame = CGRectMake(0, 0, self.frame.size.height, self.frame.size.height );
    activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    activityIndicator.backgroundColor = [UIColor clearColor];

    [self addSubview:activityIndicator];

    CGFloat labelX = activityIndicator.bounds.size.width + 2;

    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(labelX, 0.0f, self.bounds.size.width - (labelX + 2), self.frame.size.height)];
    label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    label.font = [UIFont boldSystemFontOfSize:12.0f];
    label.numberOfLines = 1;

    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    label.text = @"Loading..";

    [self addSubview:label];
}

You will also have to set methods for startAnimating, stopAnimating and one for setting the label text, but hopefully you get this idea.

, :

UIProgressViewWithLabel * pv = [[UIProgressViewWithLabel alloc] initWithFrame:CGRectMake(0,0,150,25)];

, .

+10

All Articles