【发布时间】:2012-05-29 09:10:31
【问题描述】:
我创建了 UIView 的子视图来制作一些绘图 - 绘图在我的子类的 drawRect 方法中工作正常,但是,我无法更改视图的背景颜色。一点谷歌搜索告诉我我还没有设置视图的框架,但我不完全确定如何做到这一点。我尝试了两件事:
我在 Storyboard 中创建视图并将其添加到我的视图控制器中,然后在头文件中将其声明为属性并将它们链接起来。我在实现文件的顶部合成了属性,在viewDidLoad方法中,我添加了:
[myView setBackgroundColor: [UIColor whiteColor]];
视图的背景仍然是黑色的。
我也试过了:
ViewSubclass *v = [[ViewSubclass alloc] initWithFrame: self.view.frame];
v.backgroundColor = [UIColor whiteColor];
myView = v;
无济于事。
我做错了什么?
更新:这是我用来在视图中绘制的代码,以防万一那里发生了什么事!
- (void)drawRect:(CGRect)rect {
CGFloat height = self.bounds.size.height;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
CGFloat barWidth = 30;
int count = 0;
for (NSNumber *num in samples) {
CGFloat x = count * (barWidth + 10);
CGRect barRect = CGRectMake(x, height - ([num floatValue] * height), barWidth, [num floatValue] * height);
CGContextAddRect(context, barRect);
count++;
}
CGContextFillPath(context);
}
它只是在屏幕上创建一组不同高度的条。
【问题讨论】:
-
试试 initWithFrame:CGRectMake(0,0,400,500)........或者根据你的也改变宽度和高度..看看你的属性/声明是否连接到你的视图跨度>
-
您好,感谢您的回复。不幸的是,这仍然不起作用!
标签: iphone ios xcode uiview background