简体   繁体   English

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

[英]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. 在我的浏览器应用程序中,如果响应是pdf文件,我使用NSURL连接下载文件。 When I receive data I show a UIprogressView to show the download status. 当我收到数据时,我会显示一个UIprogressView来显示下载状态。 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 在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];
}

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. 有人可以指出我是否有任何遗漏的东西。

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. viewcontroller(self.view)的主视图在其上面包含3个以上的子视图,并且由于子视图而无法更改self.view的背景颜色。 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. 并在将progressView添加到self.view之前将其添加到self.view中。

The background color of a progress view is barely visible.This is an image of a progress view with red background color: 进度视图的背景颜色几乎看不见。这是具有红色背景颜色的进度视图的图像:

进度视图

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. 如果您想要将其更改为另一种视图,或更改progressTintColor属性。

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: 我看了文档,看起来你甚至不需要像Mac OS X那样的UIBezierPath实例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM