Draw line in UIView

后端 未结 8 783
粉色の甜心
粉色の甜心 2020-12-02 05:52

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 In

8条回答
  •  有刺的猬
    2020-12-02 06:20

    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);
    }
    

提交回复
热议问题