Draw a vertical line in a UIView

UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, dialogContainer.bounds.size.height - buttonHeight - buttonSpacerHeight, dialogContainer.bounds.size.width, buttonSpacerHeight)];
lineView.backgroundColor = [UIColor colorWithRed:198.0/255.0 green:198.0/255.0 blue:198.0/255.0 alpha:1.0f];
[dialogContainer addSubview:lineView];

I used this code to draw a horizontal line on a UIView. How to add vertical line in UIView?

+4
source share
6 answers
UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(dialogContainer.bounds.size.width/2, 0, buttonSpacerHeight, dialogContainer.bounds.size.height)];
lineView.backgroundColor = [UIColor colorWithRed:198.0/255.0 green:198.0/255.0 blue:198.0/255.0 alpha:1.0f];
[dialogContainer addSubview:lineView];
+7
source

You can subclass UIView and override the drawRect: method. for example

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();
            //Horizontal Line
    CGContextSetLineWidth(context, buttonSpacerHeight);
    CGContextMoveToPoint(context,0,dialogContainer.bounds.size.height - buttonHeight - buttonSpacerHeight);
    CGContextAddLineToPoint(context,dialogContainer.bounds.size.height - buttonHeight - buttonSpacerHeight + dialogContainer.bounds.size.width,dialogContainer.bounds.size.height - buttonHeight - buttonSpacerHeight);
          //Vertical Line 
   CGContextAddLineToPoint(context,dialogContainer.bounds.size.height - buttonHeight - buttonSpacerHeight + dialogContainer.bounds.size.width, dialogContainer.bounds.size.height);
   CGContextStrokePath(context);
}

If you cannot use UIViews to draw vertical lines, reduce the width to a small value and increase the height of the UIView to your liking.

+3
source

, :)

UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, dialogContainer.bounds.size.height - buttonHeight - buttonSpacerHeight,buttonSpacerHeight, dialogContainer.bounds.size.height)];

UIView *horizontalLineView=[[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 2)];
[horizontalLineView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:horizontalLineView];


UIView *verticalLineView=[[UIView alloc] initWithFrame:CGRectMake(100, 100, 2, 100)];
[verticalLineView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:verticalLineView];

coreGraphic,

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, self.frame);

    CGContextMoveToPoint(context, XstartPoint, ystartPoint);

    CGContextAddLineToPoint(context,XendPoint,YendPoint);

    CGContextSetLineWidth(context, 2.0);

    CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);

    CGContextStrokePath(context);
}

,

+2

, - :

override func drawRect(rect: CGRect) {
    let context = UIGraphicsGetCurrentContext()
    let spacerHeight = rect.size.height

    CGContextSetLineWidth(context, 2.0)
    CGContextMoveToPoint(context, 0, (spacerHeight / 4.0))
    CGContextAddLineToPoint(context, 0, 3 * (spacerHeight / 4.0))
    CGContextSetStrokeColorWithColor(context, UIColor.whiteColor().CGColor)
    CGContextStrokePath(context)
}
+1
source

According to Apple documentation CGRect Returns a rectangle with the specified coordinates and size:

CGRect CGRectMake (
   CGFloat x,
   CGFloat y,
   CGFloat width,
   CGFloat height
);

So, try to invert what you should have as width with height

0
source

Just as you draw a horizontal line, just increase the height UIViewand decrease the width, as shown below,

UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, dialogContainer.bounds.size.height - buttonHeight - buttonSpacerHeight, 2, 200)];
0
source

All Articles