codemaster
codemaster

Reputation: 572

change background color of a view in iOS

In my browser application I am using NSURL connection to download a file if the response is a pdf file. When I receive data I show a UIprogressView to show the download status. I want to disable and change the color of background view until the download is complete.

In didReceiveResponse delegate I call a method to create progressView and change backgroundcolor and disable the parentView

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[self.fileData setLength:0];
self.totalFileSize = response.expectedContentLength;
[self performSelectorOnMainThread:@selector(startProgressView) withObject:nil waitUntilDone:NO];
}

-(void) startProgressView
{
CGSize frameSize = self.view.frame.size;
CGFloat margin = 30.0;
CGPoint center = self.view.center;

 topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, margin, frameSize.width-2*margin, 20.)];
 bottomLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, margin, frameSize.width-2*margin, 20.)];

[topLabel setText:@"downloading file"];
[topLabel setCenter:CGPointMake(center.x, center.y - 20.)];
[topLabel setTextAlignment:NSTextAlignmentCenter];

[bottomLabel setText:@"downloadstatus"];
[bottomLabel setCenter:CGPointMake(center.x, center.y + 20.)];
[bottomLabel setTextAlignment:NSTextAlignmentCenter];

self.progressBar = [[UIProgressView alloc] initWithFrame:
                    CGRectMake(0, margin, frameSize.width-2*margin, 20.)];
[self.progressBar setProgress:0];
[self.progressBar setCenter:center];

[self.view addSubview:topLabel];
[self.view addSubview:(self.progressBar)];
[self.view addSubview:bottomLabel];

 /*
CGRect frame = CGRectMake(0.0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
UIView *v = [[UIView alloc] initWithFrame:frame];
[v setBackgroundColor:[[UIColor alloc] initWithRed:255.0
                                             green:0.0
                                              blue:0.0
                                             alpha:0.1]];
[self.view insertSubview:v atIndex:0];
[v release];
*/

[self.view setBackgroundColor: [UIColor colorWithRed:0.0 green:255.0 blue:128.0/255 alpha:0.5]];
[self.view setUserInteractionEnabled:NO];
}

I tried setting background color and even inserting a new view with color but background color doesn't change. Can someone point out if there is anything that I am missing.

Upvotes: 1

Views: 9272

Answers (2)

codemaster
codemaster

Reputation: 572

Thanks all for your advise. I figured out the reason of background color not being visible. the main view of viewcontroller(self.view) contain 3 more subview on top of it, and changing the background color of self.view is not visible because of the subviews. To change the background color I just create another view with

CGRect frame = CGRectMake(0.0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
backgroundView = [[UIView alloc] initWithFrame:frame];
[backgroundView setBackgroundColor:[[UIColor alloc] initWithRed:204./255 green:213./255 blue:216./255 alpha:0.5]];
[self.view addSubview:backgroundView];

and add it to the self.view before adding the progressView to it.

Upvotes: 2

Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 21996

The background color of a progress view is barely visible.This is an image of a progress view with red background color:

progress view

Like you see the red color is only in a little portion of the image.
As for how to change it, it's enough to set it's property:

yourView.backgroundColor=[UIColor redColor];
// make it the color that you wish

If you want to see it well change it to another kind of view, or change the progressTintColor property.

EDIT

If the view hasn't something that you want to override on the background, you can freely subclass it and draw inside it. I watched the documentation, it seems like you don't even need a UIBezierPath instance like with Mac OS X:

- (void) drawRect:(CGRect)rect
{
    [[UIColor redColor] set];  
    UIRectFill(rect);
}

Upvotes: 1

Related Questions