【发布时间】:2011-03-25 05:45:05
【问题描述】:
我在 nib 文件中添加了一个UIView。我想用drawRect 方法中的颜色填充UIView。我该怎么做?
【问题讨论】:
标签: ios iphone uiview fill drawrect
我在 nib 文件中添加了一个UIView。我想用drawRect 方法中的颜色填充UIView。我该怎么做?
【问题讨论】:
标签: ios iphone uiview fill drawrect
您可以使用 UIView 的frame 属性来绘制矩形。只需将其传递给CGContextFillRect 函数(已设置上下文填充颜色)。
使用当前图形状态下的填充颜色绘制包含在提供的矩形内的区域。
所以drawRect中的代码:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext ();
// The color to fill the rectangle (in this case black)
CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0);
// draw the filled rectangle
CGContextFillRect (context, self.bounds);
}
【讨论】:
为什么不将 backgroundColor 设置为您想要的颜色填充?
【讨论】:
如果你真的需要使用drawRect:,你可以使用这个:
- (void)drawRect:(CGRect)rect {
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);
CGContextAddRect(context, CGRectMake(0, 0, 320, 460));
}
【讨论】:
在表格视图单元格的子视图中设置背景颜色会导致该视图不可见,因为启用编辑样式时,选择一个单元格会使所有背景视图颜色发生变化
【讨论】: