// Step 1 : Add QuartzCore.framework in your project
// Step 2 : Create following two files .h and .m
// Step 3 : How to Use
// Import your custom file
#import "myDrawingView.h"
// Create object and add to your viewcontroller
myDrawingView *obj = [[myDrawingView alloc] initWithFrame:self.view.frame];
[obj drawSquaresWithNumber:17 andSize:self.view.frame.size];
[self.view addSubview:obj];
//------------------------------------------------------
// filename : myDrawingView.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface myDrawingView : UIView
{
    int nSquares;
    CGSize mazeSize;
}
- (void)drawSquaresWithNumber:(int)numberOfSquares andSize:(CGSize)screenSize;
@end
//------------------------------------------------------
    // filename : myDrawingView.m
#import "myDrawingView.h"
@implementation myDrawingView
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    // Calculate height and width for each sqaure.
    float area = mazeSize.height*mazeSize.width;
    float squareSize = (area/nSquares)/100;
    NSLog(@"row : %f %f",mazeSize.width/squareSize,mazeSize.height/squareSize);
    int row, col;
    row = ceil(mazeSize.width/squareSize);
    col = ceil(mazeSize.height/squareSize);
    float height = mazeSize.height/row;
    float width = mazeSize.width/col;
    NSLog(@"%d %d",row,col);
    NSLog(@"h %f w %f",height,width);
    NSLog(@"square size : %f",squareSize);
    // Create Current Context To Draw
    CGContextRef context = UIGraphicsGetCurrentContext();
    // Draw Line
    CGContextSetLineWidth(context, 0.5f);
    CGContextSetFillColorWithColor(context, [myDrawingView randomColor].CGColor);
    CGContextSetStrokeColorWithColor(context, [myDrawingView randomColor].CGColor);
    int x ,y, cnt;
    x = y = 0;
    cnt = 1;
    // A loop for number of squares
    for(int i=0;i<row;i++)
    {
        for(int j=0;j<col;j++)
        {
            if(cnt<=nSquares)
            {
                CGRect rect = CGRectMake(x, y, width, height);
                // Draws Squares
                UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
                // To draw Oval uncomment
//                    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:rect];
                [path fill];
                [path stroke];
            }
            x += width;
            cnt++;
        }
        x = 0;
        y += height;
    }
}
+ (UIColor *) randomColor {
    CGFloat red =  arc4random()%256;
    CGFloat blue = arc4random()%256;
    CGFloat green = arc4random()%256;
    return [UIColor colorWithRed:abs(red)/255.0f green:abs(green)/255.0f blue:abs(blue)/255.0f alpha:1.0];
}
- (void)drawSquaresWithNumber:(int)numberOfSquares andSize:(CGSize)screenSize
{
    nSquares = numberOfSquares;
    mazeSize = screenSize;
    [self setNeedsDisplay];
}
@end