0

I am subclassing a UIView and am calling it via a detail view controller. When I load it, it shows up with the right dimensions and positioning, however it is just a black square... When I just put a UIView into interface builder with the class, it works. I don't think draw rect is called. Heres my code:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setNeedsDisplay];
    }
    return self;
}


- (void)drawRect:(CGRect)rect
{
    // Drawing code

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 255.0, 255.0, 0, 1);
    CGContextStrokeRect(context, CGRectMake(0, 0, 50, 50));
}

and I call it like so:

 GameBox *boxy = [[GameBox alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
    [self.view addSubview: boxy];
STW
  • 44,917
  • 17
  • 105
  • 161
michaela
  • 173
  • 1
  • 2
  • 13
  • why are you calling setneedsdisplay in init? this is incorrect, at the point of init it hasnt been added to any view, and the view has not loaded.. – Daniel Apr 27 '12 at 19:23
  • Then how do I get the thing to show up not black? I have tried it without setting setneedsdisplay – michaela Apr 27 '12 at 19:24
  • first the color is betwen 0 and 1, not 255... – Daniel Apr 27 '12 at 19:24

1 Answers1

2

You are setting your fill color but you are stroking a rect. You need to set the stroke color: CGContextSetRGBStrokeColor

Tim Reddy
  • 4,340
  • 1
  • 40
  • 77