2012-12-12 97 views
3

我在我的drawRect下面的代码:背景颜色

CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
CGContextFillPath(context); 

我基本上是想成立这个观点的背景是红色的。但是上面的代码中并没有做到这一点。我究竟做错了什么?

+0

@PaulPerry的回答是一个很好的答案。但是如果你需要现有代码的帮助,你应该发布基于UIView类的完整'drawRect:'方法。用完整的方法更新你的问题。 – rmaddy

回答

2

你可能想:

CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
CGContextFillRect(context, self.bounds); 

CGContextFillPath填补了一个“路径”,你必须先建立一系列其他的呼叫,李ke CGContextAddRectCGContextAddArc。只是填充矩形比较容易。

5

更简单的方法来改变视图的背景颜色是:

view.backgroundColor = [UIColor redColor]; 

或者如果你是在视图控制器:

self.view.backgroundColor = [UIColor redColor]; 
1
[[UIColor redColor] setFill]; 
CGContextRef ref= UIGraphicsGetCurrentContext(); 
CGContextFillRect(ref, self.bounds);