在iOS故事板中绘制类似于Android的XML行的行

Kat*_*lon 6 line uiview ios uistoryboard

在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>
Run Code Online (Sandbox Code Playgroud)

这是Android中的常见做法.我怎么能在iOS中做同样的事情?常见的做法是什么?我在这里看到另一个问题,试图问一个类似的问题,但被告知要使用TableView.我正在寻找像Android答案一样简单和通用的东西.

Tau*_*aum 23

您可以创建通用UIView并在故事板中将其设置为1 widthheight1磅.将其设置backgroundColor为您想要的行.确保设置其约束或调整大小,以便在调整屏幕大小时不会增加宽度/高度.


小智 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];
Run Code Online (Sandbox Code Playgroud)

使用在代码中创建的这个简单视图,只需将其高度调整为1或2就像在上面的代码中一样 - > lineView.frame = CGRectMake(<#CGFloat x#>,<#CGFloat y#>,<#CGFloat width#>, <#CGFloat height#>); CGFloat的高度为1