简体   繁体   中英

Draw Vertical line in 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 have used this code to draw horizontal line on UIView. How can I add Vertical line to UIView?

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];

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 are adamant to use UIViews itself to draw vertical lines then reduce the width to a negligible value and increase the height of the UIView according to your wish.

Just exchange your height and width to draw vertical line simple :)

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

other example

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];

If you want to use coreGraphic then

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

If you want to draw using sprite kit then follow

taking Benny's answer to swift, you could do something like:

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

According to Apple's documentation CGRect Returns a rectangle with the specified coordinate and size values:

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

So try inverting what you ised to have as the width with the height

就像你有绘制水平线只是增加UIView的高度,并减小宽度,如下所示,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM