I have a view controller based app that contains several views that I display/hide depending on some logic. I would like to draw a rectangle which will be the size of the UIView in order to give it like a frame/border shape.
I’m having issue drawing a rectangle. I understand that the following code should do it but I’m not sure why this method is not being called or triggered. I also didn’t see the (void)drawRect:(CGRect)rect method generated anywhere so I placed it myself. Not sure what I’m missing here..
- (void)drawRect:(CGRect)rect;
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 0.0, 1.0); // yellow line
CGContextBeginPath(context);
CGContextMoveToPoint(context, 50.0, 50.0); //start point
CGContextAddLineToPoint(context, 250.0, 100.0);
CGContextAddLineToPoint(context, 250.0, 350.0);
CGContextAddLineToPoint(context, 50.0, 350.0); // end path
CGContextClosePath(context); // close path
CGContextSetLineWidth(context, 8.0); // this is set from now on until you explicitly change it
CGContextStrokePath(context); // do actual stroking
CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 0.5); // green color, half transparent
CGContextFillRect(context, CGRectMake(20.0, 250.0, 128.0, 128.0)); // a square at the bottom left-hand corner
}
If all you need to do is add a simple rectangular border, just do the following in your
viewWillAppear:for your view controller.Hope this helps!