2013-10-09 3 views
4
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]; 

Я использовал этот код для рисования горизонтальной линии на UIView. Как добавить вертикальную строку в UIView?Ничья Вертикальная линия в UIView

+0

Измените 'методы CGRectMake' для создания вертикального окна? Если вы делаете кучу пользовательского чертежа, вы действительно должны использовать 'drawRect:' https://developer.apple.com/library/ios/documentation/2ddrawing/conceptual/drawingprintingios/graphicsdrawingoverview/graphicsdrawingoverview.html – BergQuester

+0

http : //stackoverflow.com/questions/19092011/how-to-draw-a-line-in-sprite-kit/19092449#19092449 – Rajneesh071

+0

По какой-либо причине никто не рискует использовать CALayer? –

ответ

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

Добавление подзаголовка - это легкий выход, но вы на самом деле не рисуете линию, а скорее ее подвид, который не является идиоматическим. –

+0

Я только что ответил на вопрос OP, как он выбрал @JeslyVarghese –

0

Согласно документации Apple, CGRect Возвращает прямоугольник с заданными координатами и размерными значениями:

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

Так попробуйте инвертировать, что вы ароматизированные, чтобы иметь ширину с высотой

2

Просто обменивать свой рост и ширина нарисовать вертикальную линию простой :)

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

Если вы хотите нарисовать, используя набор спрайтов затем follow

+0

Г-н Тайлор ... разве вы не думаете, что вы дали столь проработанный ответ? –

+2

@VineetSingh - Да, я всегда стараюсь разрабатывать вещи ... :) – Rajneesh071

0

Как вы должны нарисовать горизонтальную линию просто увеличить высоту UIView и и уменьшить ширину, как показано ниже,

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

Вы можете подклассифицировать UIView и переопределить метод drawRect :. Например

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

Если вы непреклонны использовать сам UIViews рисовать вертикальные линии, то уменьшить ширину до пренебрежимо малой величины и увеличить высоту UIView по вашему желанию.

+0

Из документа Apple вам не нужно называть супер для 'drawRect:' –

1

принимая ответ Бенни к быстрым, вы могли бы сделать что-то вроде:

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) 
} 
Смежные вопросы