开发者

UIView drawRect does not draw anything for me?

开发者 https://www.devze.com 2022-12-16 05:41 出处:网络
I made a UIViewController class. Displays fine. I then made a UIView class to use with it. In InterfaceBuilder, I set the UIView class to my own custom UIView class. I then overrode the drawRect metho

I made a UIViewController class. Displays fine. I then made a UIView class to use with it. In InterfaceBuilder, I set the UIView class to my own custom UIView class. I then overrode the drawRect method in that class:

- (void)drawRect:(CGRect)rect 
{
    NSLog(@"Yes we are drawing with width: %f", rect.size.width);

    CGContextRef g = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(g, 1, 1, 1, 1);
    CGContextSetRGBFillColor(g, 1, 1, 1, 0);
    CGContextFillRect(g, CGRectMake(0, 0, 100, 100));

and I can see that the log statement prints, however none of my drawing code actually has 开发者_Python百科any visible effect. The rect does not get drawn on screen. Is there some other connection I had to make in InterfaceBuilder for this to work?

Other than setting the class name for the UIView property of my view controller in InterfaceBuilder, I haven't changed anything else.

Thanks


The last argument to CGContextSetRGBFillColor is the alpha of the fill color. You're setting it to zero, so you're filling with a transparent color. Try this:

- (void)drawRect:(CGRect)rect 
{
    NSLog(@"Yes we are drawing with width: %f", rect.size.width);

    CGContextRef g = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(g, 1, 1, 1, 1);
    CGContextSetRGBFillColor(g, 1, 1, 1, 1);
    CGContextFillRect(g, CGRectMake(0, 0, 100, 100));
...
0

精彩评论

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

关注公众号