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に垂直線を追加するにはどうすればよいですか?

4

6 に答える 6

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];
于 2013-10-09T05:45:55.737 に答える
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);
}

スプライト キットを使用して描画する場合は、次の手順に従ってください

于 2013-10-09T05:45:49.033 に答える
1

Swift に対する Benny の回答を参考にすると、次のようなことができます。

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)
}
于 2016-01-03T14:22:46.150 に答える
0

Apple のドキュメントによると CGRect 指定された座標とサイズの値を持つ長方形を返します。

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

したがって、幅と高さを逆にしてみてください

于 2013-10-09T05:45:08.343 に答える
0

水平線を描いたUIViewように、以下のように高さを増やして幅を減らします。

UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, dialogContainer.bounds.size.height - buttonHeight - buttonSpacerHeight, 2, 200)];
于 2013-10-09T05:46:35.273 に答える