簡體   English   中英

帶有UILabel的自定義視圖顯示為黑色

[英]Custom View with UILabel in it appears black

我創建了一個自定義UIView,其中包含一組UILabel。 我將這些UILabel添加到其drawRect方法內的自定義視圖中。

但是整個自定義視圖在屏幕上顯示為黑色。 我尚未為自定義UIView設置任何背景色。 我該如何解決?

我嘗試將背景色設置為ClearColor,但它仍然看起來是黑色的。

我嘗試將自定義視圖的opaque屬性設置為false,該視圖顯然消失了。

請幫忙。

在此處輸入圖片說明

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];

不要在drawRect:方法中執行此操作,該方法旨在在圖形上下文中進行繪制。 如果要添加一些特定的子視圖,請在init / initWithFrame方法中進行。 對我而言,最好的方法是創建一個自定義uiviewcontroller子類,並使用xib(nib)文件對其進行初始化。 在控制器級別工作是一個好習慣。

暫無
暫無

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

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