Question

I'm using a draw rect to draw rectangle with a corner radius. The code works perfectly. When viewed on a 4inch screen you see the rectangle. When viewed on a 3.5inch screen the rectangle does not resize and runs off the bottom of the screen. Any pointers?

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGRect rrect = CGRectMake(20, 30, 280, 515);
    CGFloat radius = 7;

    CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect);

    // Start at 1
    CGContextMoveToPoint(context, minx, midy);
    // Add an arc through 2 to 3
    CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
    // Add an arc through 4 to 5
    CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
    // Add an arc through 6 to 7
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    // Add an arc through 8 to 9
    CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);

    // Close the path
    CGContextClosePath(context);

    CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]);
    CGContextSetRGBFillColor(context, 255, 255, 255, 1.0);

    // Fill & stroke the path
    CGContextDrawPath(context, kCGPathFillStroke);

}
Was it helpful?

Solution

CGRect rrect = CGRectMake(20, 30, 280, 515);

This is the problem. You draw your rectangle with a height of 515 starting at an offset of 20, while your screen is not that long. Try using the frame of your view to draw instead of hardcoded values

CGRect rrect = CGRectMake(20, 30, self.frame.size.width - 20*2, self.frame.size.height - 30*2);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top