Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing a very thin line on UIView

So I got UIView at the top of my app and I want to draw a thin line at the bottom of it, a very similar line that act as a seperator in UITableView. I currently have a UIView that has height of 1px set in IB, but when I compare that line to the seperator in UITableView it has higher height. Is there any good way to draw a thin line inside a UIView?

like image 422
Samuli Lehtonen Avatar asked Mar 19 '14 07:03

Samuli Lehtonen


2 Answers

Give the frame of your line view a height (or width) of 0.5 and set the backgroundColor to [UIColor lightGrayColor].

Edit: For non-retina you might change the height/width to (1.0 / [UIScreen mainScreen].scale)

This will result in 0.5 on retina and 1.0 on non-retina displays.

like image 64
Marc Avatar answered Sep 18 '22 17:09

Marc


Just insert inside your UIView

#import <QuartzCore/QuartzCore.h>

and inside drawRect: add this and specify the height of the line

- (void)drawRect:(CGRect)rect{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2.0f);
    //start at this point
    CGContextMoveToPoint(context, 0, self.frame.size.height);
    //draw to this point
    CGContextAddLineToPoint(context,
                            self.frame.size.width,
                            self.frame.size.height);
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    // and now draw the Path!
    CGContextStrokePath(context);

}

and you'll get a line like the red one here

enter image description here

like image 22
Alex Cio Avatar answered Sep 20 '22 17:09

Alex Cio