Drawing a very thin line on a UIView

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

+4
source share
3 answers

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

: - / (1.0 / [UIScreen mainScreen].scale)

0,5 1,0 .

+8

UIView

#import <QuartzCore/QuartzCore.h>

drawRect:

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

}

, ​​ ,

enter image description here

+3

Try this to view. this code will draw a red 2px frame for your look.

Add #import <QuartzCore/QuartzCore.h>to your mind.

CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0.0f, myview.frame.size.height - 2, myview.frame.size.width, 2.0f);
bottomBorder.backgroundColor = [UIColor redColor].CGColor;
[myview.layer addSublayer:bottomBorder];
+1
source

Source: https://habr.com/ru/post/1532437/


All Articles