0

Here is my drawRect:

- (void)drawRect:(CGRect)rect
{
    CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), self.bounds);
}

and the instances of this class are added a subviews the next way:

(#define DOTS_SIZE 30)

[self addSubview:[[VertexView alloc] initWithFrame:CGRectMake(anchor.x-DOTS_SIZE/2, anchor.y-DOTS_SIZE/2, DOTS_SIZE, DOTS_SIZE)]];

As far as I understand, I should get and ellipse (circle my case) in the views bounds. But I get it fully filled with rectangle (square).

By the way, I have logged bounds and their size is 30x30, so I should get nice little circles, but I get squares (T_T)

I'll be thankful for any advise!

Uko
  • 13,134
  • 6
  • 58
  • 106
  • 1
    You should post more code and preferably the complete `drawRect` method. The problem is not in the line you posted. – sch Mar 17 '12 at 20:42
  • @sch I've edited my question, but anyway there is only one method in my `drawRect`. I just need to set a circle to represent vertex. – Uko Mar 17 '12 at 21:37

1 Answers1

4

The problem is that you have to make some setup before drawing the ellipse. For example:

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor); // Or any other color.
    CGContextFillEllipseInRect(ctx, self.bounds);
}

And to make background transparent you can check out: Setting A CGContext Transparent Background

Community
  • 1
  • 1
sch
  • 27,436
  • 3
  • 68
  • 83
  • wow. that's really a solution, but why this views start with black background? Their superview starts with white. And my first implementation was to just draw circles instead of instantiating the subviews. That's why I was confused, because I've thought that all views start with a white background – Uko Mar 17 '12 at 21:54
  • Yes, views has a property `backgroundColor` that is initially equal to the white color. But when you implement `drawRect:` the background color is not taken into consideration and you are responsible for drawing your view. – sch Mar 17 '12 at 21:57