在iOS中更改视图的背景颜色

时间:2012-11-15 20:30:38

标签: iphone objective-c ios

在我的浏览器应用程序中,如果响应是pdf文件,我使用NSURL连接下载文件。 当我收到数据时,我会显示一个UIprogressView来显示下载状态。 我想禁用并更改背景视图的颜色,直到下载完成。

在didReceiveResponse委托中,我调用一个方法来创建progressView并更改backgroundcolor并禁用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];
}

我尝试设置背景颜色,甚至插入带有颜色的新视图,但背景颜色不会改变。 有人可以指出我是否有任何遗漏。

2 个答案:

答案 0 :(得分:2)

感谢大家的建议。我弄清楚背景颜色不可见的原因。 viewcontroller(self.view)的主视图在其上面包含3个以上的子视图,并且由于子视图而无法更改self.view的背景颜色。 要更改背景颜色,我只需使用

创建另一个视图
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];

并在将progressView添加到self.view之前将其添加到self.view中。

答案 1 :(得分:1)

进度视图的背景颜色几乎看不见。这是带有红色背景颜色的进度视图的图像:

progress view

就像你看到红色仅在图像的一小部分中 至于如何改变它,它足以设置它的属性:

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

如果您希望将其更改为其他类型的视图,或更改progressTintColor属性。

修改

如果视图没有要在背景上覆盖的内容,则可以自由地将其子类化并在其中绘制。 我看了文档,看起来你甚至不需要像Mac OS X那样的UIBezierPath实例:

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