UIView填充颜色

Pri*_*iya 3 iphone fill uiview drawrect ios

我已经UIView在nib文件中添加了一个.我想UIViewdrawRect方法填充颜色.我怎样才能做到这一点?

Red*_*ing 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);
}
Run Code Online (Sandbox Code Playgroud)