iphone - 在 iOS 中更改 View 的背景颜色

标签 iphone objective-c ios

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

在 didReceiveResponse 委托(delegate)中,我调用了一个方法来创建 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];
}

我尝试设置背景颜色,甚至插入一个带有颜色的新 View ,但背景颜色没有改变。
有人可以指出我是否缺少任何东西。

最佳答案

谢谢大家的建议。我找出了背景颜色不可见的原因。
viewcontroller(self.view) 的主视图在其顶部包含另外 3 个 subview ,并且由于 subview ,更改 self.view 的背景颜色是不可见的。
要更改背景颜色,我只需创建另一个 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];

并将其添加到 self.view 中,然后再将 progressView 添加到其中。

关于iphone - 在 iOS 中更改 View 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13405659/

相关文章:

ios - UIView 'falling rain' 透明叠加可以点进去?

ios - 代码自动出现在 viewDidUnload 中

ios - 谷歌分析跟踪没有遗产

ios - Swift:如何在没有 topBorder 的情况下向 UITabBarController 添加阴影

ios - 是否可以将应用程序商店中的应用程序切换为私有(private)(企业)?

iphone - 如何访问 NSString 中的第 n 个字符?

iphone - 将触摸事件传递给 View Controller 的所有 subview

iphone - NSURLConnection从youtube下载视频

iphone - 用于下拉菜单的 UIPickerview

ios - 神秘的核心数据故障崩溃