2011-12-27 77 views
0

我創建了一個自定義的UIView,其中有一些UILabels。我在自己的drawRect方法中添加這些UILabels。UILabel自定義視圖中顯示爲黑色

但整個自定義視圖在屏幕上顯示爲黑色。我沒有爲自定義UIView設置任何背景顏色。我該如何解決?

我試着將背景顏色設置爲ClearColor,但它仍然看起來黑。

我試圖將自定義視圖的不透明屬性設置爲false,視圖明顯消失。

請幫忙。

enter image description here

+0

您是否將UILabel的背景顏色設置爲黑色? – johnluttig 2011-12-27 07:29:59

+0

發佈一些代碼.. – Legolas 2011-12-27 08:37:03

回答

3

不這樣做,在的drawRect:其旨在方法在圖形上下文繪製。如果你想添加一些特定的子視圖,可以在init/initWithFrame方法中進行。 對我來說,最好的方法是創建一個自定義的uiviewcontroller子類並使用xib(nib)文件對其進行初始化。在控制器層面工作是一種很好的做法。

3
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(10,0,320,35)]; 
newView.backgroundColor=[UIColor clearColor]; 

UILabel *mytext = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 28.0)]; 
mytext.backgroundColor = [UIColor clearColor]; 
mytext.textColor = [UIColor blackColor]; 
mytext.editable = NO; 
mytext.text = @"Your label"; 
[newView addSubview:mytext]; 
[mytext release]; 
[self.view addSubview:newView]; 
[newView release];