UIView背景颜色

时间:2010-02-16 22:38:17

标签: iphone uiview

我正在尝试使用UITableView创建网格视图(我的旧问题已经指向了这个方向)并且我正在为各个项目设置视图。创建了一个每行显示3个项目的自定义UITableViewCell后,我决定将这些项目放入一个名为ItemView的UIView子类中。

然后将此ItemView作为子视图添加到自定义UITableViewCell以显示网格。无论如何,我已经设法创建了视图,并且可以让它显示UILabel罚款,但是我无法将ItemView更改为透明,而不是其中的UIViews(标签,按钮,图像等)。这是我的UIView代码:

#import <UIKit/UIKit.h>
@interface ItemView : UIView {
}
@end

#import "ItemView.h"
@implementation ItemView

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
  [self setBackgroundColor:[UIColor lightGrayColor]];
}

- (void)dealloc {
  [super dealloc];
}
@end

我应该在哪里设置背景颜色才能正常工作?

干杯

1 个答案:

答案 0 :(得分:11)

尝试:

self.backgroundColor = [UIColor clearColor];

我认为你的init方法没有任何理由不这样做。我认为您不想覆盖drawRect:在这种情况下,您需要自己绘制整个视图。

相关问题