bernie2436
bernie2436

Reputation: 23941

having trouble drawing a rectangle to the screen ios

I am following a video tutorial from pluralsight that draws a red rectangle to the screen. I have a subclass of UIView called PSViewDemo that has the following code in the .m file:

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColor(context, [UIColor redColor].CGColor);
    CGContextFillRect(context, CGRectMake(40, 400,100,200));
    // Drawing code
}

I call it in the Viewdidload of the viewcontroller for the only view in the application (until the code adds the subview).

- (void)viewDidLoad
{

    [super viewDidLoad];
    PSViewDemo *dv = [[PSViewDemo alloc] initWithFrame:CGRectMake( 0, 0, 320, 480)];
    [self.view addSubview:dv];
    // Do any additional setup after loading the view, typically from a nib.
}

The whole thing compiles and runs without error but there is no red rectangle on the screen.

What am I missing? I am pretty sure I am following the tutorial exactly so maybe something has changed in Cocoa since the tutorial was made? I'm using xCode 5.

Upvotes: 0

Views: 128

Answers (1)

Dani-san
Dani-san

Reputation: 296

Change

CGContextSetFillColor(context, [UIColor redColor].CGColor);

To

CGContextSetFillColor(context, CGColorGetComponents([[UIColor redColor] CGColor]));

Upvotes: 2

Related Questions