在Android中,我可以通过简单创建视图和设置背景颜色来绘制线条
<LinearLayout...>
...
<View android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/black"
...
<View android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/red"
...
</LinearLayout>
这是Android中的常见做法。我怎么能在iOS中做同样的事情?常见的做法是什么?我在这里看到另一个问题,试图问一个类似的问题,但被告知要使用TableView。我正在寻找像Android答案一样简单和通用的东西。
答案 0 :(得分:23)
您可以创建通用UIView
并在故事板中将其width
或height
设置为1 pt。将其backgroundColor
设置为您想要的行。确保设置其约束或调整蒙版大小,以便在调整屏幕大小时它不会在宽度/高度上增长。
答案 1 :(得分:9)
UIView *lineView = [[UIView alloc]init];
lineView.frame = CGRectMake(0,50,self.View.frame.size.width,1);
lineView.backgroundColor = [UIColor blackColor];
[self.view addSubview:lineView];
使用在代码中创建的这个简单视图,只需将其高度调整为1或2即可 就像在上面的代码中一样 - &gt; lineView.frame = CGRectMake(&lt; #CGFloat x#&gt;,&lt; #CGFloat y#&gt;,&lt; #CGFloat width#&gt;,&lt; #CGFloat height#&gt;); CGFloat的高度为1
答案 2 :(得分:4)
同样的事情。使用宽度较小的UIView
并将其背景颜色设置为所需的颜色。
答案 3 :(得分:1)
//在uiview类中绘制线条
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 1.0);
CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGContextMoveToPoint(context, xstartPoint, yStart);
CGContextAddLineToPoint(context, xstartPoint, yBottom);
CGContextStrokePath(context);
CGContextSaveGState(context);