在 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.
我没有使用界面生成器.
I am NOT using Interface Builder.
推荐答案
在您的情况下(水平线)最简单的方法是添加具有黑色背景颜色和框架的子视图 [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屋!