RobertvdBerg
RobertvdBerg

Reputation: 187

iOS drawRect with custom height

I have placed a view on a viewController and I want to draw a rectangle in this view, but with a custom height. I determine the height based on a parameter in my viewController. So for example. If my parameter is 50, I want the rectangle to have the height of 50% of the UIView.

2 questions:

I have placed the view using Interface Builder and I have implemented the drawRect in a subclass of UIView and used this as Custom Class for my UIView.

So in the custom class I have:

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();

    UIColor * lightGrayColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0];

    CGRect paperRect =  self.bounds;

    drawLinearGradient(context, paperRect, lightGrayColor.CGColor, [UIColor clearColor].CGColor);


}

void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor)
{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat locations[] = { 0.0, 1.0 };

    NSArray *colors = @[(__bridge id) startColor, (__bridge id) endColor];

    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);

    CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
    CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));

    CGContextSaveGState(context);
    CGContextAddRect(context, rect);
    CGContextClip(context);
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
    CGContextRestoreGState(context);

    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);
}

This draws a nice gradient rectangle in my view but it fills the complete UIView. This is because of the self.bounds.

I have added a property *height to my custom class, but I don't know how to fill this from my viewController. So I want it to start at the bottom off the UIView, and make it as high as I have determined (a % of the real height).

Anybody knows how I can achieve this?

Upvotes: 0

Views: 924

Answers (2)

RTasche
RTasche

Reputation: 2644

have you set your custom view class in identity inspector in interface builder?

you can set the height property from your viewController:

((MyViewClass *) self.myView).height = myCalculatedValue;

then implement drawRect:

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();

    UIColor * lightGrayColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0];

    CGRect paperRect =  self.bounds;
    paperRect.origin.y = paperRect.size.height - self.height;
    paperRect.size.height = self.height;
    //assuming your height property is of CGFloat type

    drawLinearGradient(context, paperRect, lightGrayColor.CGColor, [UIColor clearColor].CGColor);
}

this will draw your gradient from (height) points above the bottom to the bottom

Upvotes: 1

carbonr
carbonr

Reputation: 6067

I think you can get calculate 50% of screen height and then substract new view height from the full screen Height

youParameter = 50; // Lets assume this is your parametere

int heightOfView   = ([UIScreen mainScreen].bounds.size.height * yourParameter) / 100;

// For placing the view to the bottom;

CGRect newFrame;
frame.origin.y = [UIScreen mainScreen].bounds.size.height - heightOfView;
frame.origin.x = 0;
frame.size.width = [UIScreen mainScreen].bounds.size.width; // assuming it will take full width
frame.size.height = heightOfView;

youViewToChange.frame = newFrame; // normally do this or

To pass the value to drawRect you can do:

[self drawRect:newFrame];

Upvotes: 1

Related Questions