Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw line in UIView

Tags:

ios

iphone

uiview

I need to draw a horizontal line in a UIView. What is the easiest way to do it. For example, I want to draw a black horizontal line at y-coord=200.

I am NOT using Interface Builder.

like image 278
John Smith Avatar asked Jun 27 '10 20:06

John Smith


1 Answers

Maybe this is a bit late, but I want to add that there is a better way. Using UIView is simple, but relatively slow. This method overrides how the view draws itself and is faster:

- (void)drawRect:(CGRect)rect {     [super drawRect:rect];      CGContextRef context = UIGraphicsGetCurrentContext();     CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);      // Draw them with a 2.0 stroke width so they are a bit more visible.     CGContextSetLineWidth(context, 2.0f);      CGContextMoveToPoint(context, 0.0f, 0.0f); //start at this point      CGContextAddLineToPoint(context, 20.0f, 20.0f); //draw to this point      // and now draw the Path!     CGContextStrokePath(context); } 
like image 200
b123400 Avatar answered Oct 22 '22 19:10

b123400