在UIView中画线 [英] Draw line in UIView
问题描述
我需要在UIView中绘制一条水平线。最简单的方法是什么。例如,我想在y-coord = 200处绘制黑色水平线。
I need to draw a horizontal line in a UIView. What is the easiest way to do it. For example, I want to draw a black horizontal line at y-coord=200.
我不使用Interface Builder。
I am NOT using Interface Builder.
推荐答案
在你的情况下(水平线)最简单的方法是添加一个黑色背景颜色和框架<$ c $的子视图c> [0,200,320,1] 。
The easiest way in your case (horizontal line) is to add a subview with black background color and frame [0, 200, 320, 1]
.
代码示例(我希望没有错误 - 我在没有Xcode的情况下编写了它) :
Code sample (I hope there are no errors - I wrote it without Xcode):
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, self.view.bounds.size.width, 1)];
lineView.backgroundColor = [UIColor blackColor];
[self.view addSubview:lineView];
[lineView release];
// You might also keep a reference to this view
// if you are about to change its coordinates.
// Just create a member and a property for this...
另一种方式是创建一个将在其drawRect方法中绘制一条线的类(您可以看到我的代码示例这里)。
Another way is to create a class that will draw a line in its drawRect method (you can see my code sample for this here).
这篇关于在UIView中画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!