开发者

UIView Fill color

开发者 https://www.devze.com 2023-02-19 10:22 出处:网络
I have added a UIView to the nib file. I want to fill that UIView with a color in drawRect method. How c开发者_C百科an i do this?You can use the frame property of the UIView to draw your rectangle. Ju

I have added a UIView to the nib file. I want to fill that UIView with a color in drawRect method. How c开发者_C百科an i do this?


You can use the frame property of the UIView to draw your rectangle. Just pass it to the CGContextFillRect function (having set the context fill color).

Paints the area contained within the provided rectangle, using the fill color in the current graphics state.

So the code in 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);
}


Why not just set backgroundColor to be the color fill you desire?


You can use this if you really need to use drawRect: :

- (void)drawRect:(CGRect)rect {
    CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
    CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);
    CGContextAddRect(context, CGRectMake(0, 0, 320, 460));
}


Setting background colour in table view cell's sub view lead to invisibility of that view due the fact that on editing style enabled, selecting a cell makes all background view colour change

0

精彩评论

暂无评论...
验证码 换一张
取 消