UIView背景颜色

Jac*_*ack 5 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
Run Code Online (Sandbox Code Playgroud)

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

干杯

Rob*_*nes 11

尝试:

self.backgroundColor = [UIColor clearColor];
Run Code Online (Sandbox Code Playgroud)

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