簡體   English   中英

在iOS中更改視圖的背景顏色

[英]change background color of a view in 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];
}

我嘗試設置背景顏色,甚至插入帶有顏色的新視圖,但背景顏色不會改變。 有人可以指出我是否有任何遺漏的東西。

謝謝大家的建議。 我弄清楚背景顏色不可見的原因。 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中。

進度視圖的背景顏色幾乎看不見。這是具有紅色背景顏色的進度視圖的圖像:

進度視圖

就像你看到紅色只在圖像的一小部分。
至於如何改變它,它足以設置它的屬性:

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM