Saturday, July 16, 2016

Code Snippet: UIView Checkered Background Pattern

    float boxes = self.view.frame.size.width/10.0;
    CGRect rect = CGRectMake(0, 0, 2*boxes, 2*boxes);
    UIGraphicsBeginImageContext(rect.size); CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect rect1 = CGRectMake(0, 0, 2*boxes, 2*boxes);
    CGRect rect2 = CGRectMake(0, 0, boxes, boxes);
    CGRect rect3 = CGRectMake(boxes, boxes, boxes, boxes);
    CGContextSetRGBFillColor(ctx, 0.5, 0.5, 0.5, 1.0);
    CGContextFillRect(ctx, rect1);
    CGContextSetRGBFillColor(ctx, 0.4, 0.4, 0.4, 1.0);
    CGContextFillRect(ctx, rect2);
    CGContextFillRect(ctx, rect3);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    self.view.backgroundColor = [UIColor colorWithPatternImage:img];