title: Unable to scale shapes to match the dimensions of UIView
body: <p>I have created a custom view that draws shapes of varying sides. The view is added to main view as a subview as given below. The shapes are of different dimensions. </p>
  
  <p><a href="https://i.stack.imgur.com/E5Y7S.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E5Y7S.png" alt="Multiple UIView shapes in a subview"></a></p>
  
  <p><a href="https://i.stack.imgur.com/vskfh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vskfh.png" alt="Individual Subviews"></a></p>
  
  <p>My source code is given below</p>
  
  <pre><code>-(instancetype) initWithSides:(NSUInteger) sides andFrame:(CGRect)frame {
          self = [super initWithFrame:frame];
          if (self) {
              [self setBackgroundColor:[UIColor clearColor]];
              self.sides = sides;
              self.radius =  CGRectGetWidth(self.frame) * 1.5 / sides);
          }
          return self;
  }
  
      -(void) drawRect:(CGRect) frame {
  // Sides is passed as constructor argument. 4 sides means a quadrilateral etc.
  // Get CGPAthRef instance
  
          CGFloat angle = DEGREES_TO_RADIANS(360 / ((CGFloat) self.sides));
      int count = 0;
      CGFloat xCoord = CGRectGetMidX(self.frame);
      CGFloat yCoord = CGRectGetMidY(self.frame);
      while (count &lt; self.sides) {
          CGFloat xPosition =
              xCoord + self.radius * cos(angle * ((CGFloat) count));
          CGFloat yPosition =
              yCoord + self.radius * sin(angle * ((CGFloat) count));
  
          [self.points addObject:[NSValue valueWithCGPoint:CGPointMake(xPosition, yPosition)]];
  
          count ++;
      }
  
      NSValue* v = [self.points firstObject];
      CGPoint first = v.CGPointValue;
  
      CGMutablePathRef path = CGPathCreateMutable();
       CGPathMoveToPoint(path, nil, first.x, first.y);
  
      for (int ix = 1; ix &lt; [self.points count]; ix++) {
          NSValue* pValue = [self.points objectAtIndex:ix];
          CGPoint p = pValue.CGPointValue;
          CGPathAddLineToPoint(path, nil, p.x, p.y);
      }
  
      CGPathCloseSubpath(path);
  
  
     CGContextRef context = UIGraphicsGetCurrentContext();
      CGContextAddPath(context, path);
      [self colourView:context withPath:path];
    }
  
  -(void) colourView:(CGContextRef) context withPath:(CGPathRef) ref {
      NSUInteger num = arc4random_uniform(8) + 1;
      UIColor* color = nil;
      switch (num) {
          case 1:
              color = [UIColor redColor];
              break;
          case 2:
              color = [UIColor greenColor];
              break;
          case 3:
              color = [UIColor yellowColor];
              break;
          case 4:
              color = [UIColor blueColor];
              break;
          case 5:
              color = [UIColor orangeColor];
              break;
          case 6:
              color = [UIColor brownColor];
              break;
          case 7:
              color = [UIColor purpleColor];
              break;
          case 8:
              color = [UIColor blackColor];
              break;
          default:
              break;
      }
  
      CGContextSetFillColorWithColor(context, color.CGColor);
      CGContextFillPath(context);
  }
  </code></pre>
  
  <p>This constitutes one single shape. This is how I am drawing the rest of the views.</p>
  
  <pre><code>-(void) initDrawView {
      NSUInteger width = CGRectGetWidth(self.view.bounds) / 8;
      NSUInteger height = (CGRectGetHeight(self.view.frame))/ 8;
      UITapGestureRecognizer *singleFingerTap =
          [[UITapGestureRecognizer alloc] initWithTarget:self
                                              action:@selector(handleSingleTap:)];
      for (int i = 0 ; i &lt; 8; i++) {
          CGFloat yCoord = i * height + i * 15;
          for (int j = 0; j &lt; 8; j ++) {
              int side = 3 + arc4random() % 8;
              CGFloat xCoord = j * width;
  
              SimpleRegularPolygonView* view =
                  [[SimpleRegularPolygonView alloc] initWithSides:side andFrame:CGRectMake(xCoord, yCoord, width, height)];
              [view sizeToFit];
              view.viewEffectsDelegate = self;
              [view setTag: (8 * i + j)];
              [self.view addGestureRecognizer:singleFingerTap];
              [self.view addSubview:view];
          }
      }
  }
  </code></pre>
  
  <p>1) I don't know how to make them have the same dimensions.  How can I do that? (First image)</p>
  
  <p>2) the images don't scale up to the size of the UIView (Second image).</p>