3

UIView在 nib 文件中添加了一个。我想用方法UIView中的颜色填充它drawRect。我怎样才能做到这一点?

4

4 回答 4

9

您可以使用 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);
}
于 2011-03-25T05:52:28.130 回答
3

为什么不将 backgroundColor 设置为您想要的颜色填充?

于 2011-03-25T05:51:06.753 回答
0

如果您确实需要使用,可以使用它drawRect:

- (void)drawRect:(CGRect)rect {
    CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
    CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);
    CGContextAddRect(context, CGRectMake(0, 0, 320, 460));
}
于 2011-03-25T06:04:00.993 回答
0

在表格视图单元格的子视图中设置背景颜色会导致该视图不可见,因为在启用编辑样式时,选择一个单元格会使所有背景视图颜色发生变化

于 2016-02-29T15:18:24.663 回答