Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw lines in iOS storyboard similar to Android's XML lines

In Android I can draw lines by simple creating a view and setting its background color

<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>

This is a common practice in Android. How might I do the same in iOS? What's the common practice? I see another question here trying to ask a similar question but was told to use a TableView. I am looking for something as simple and as general as the Android answer.

like image 679
Katedral Pillon Avatar asked Jul 10 '14 20:07

Katedral Pillon


4 Answers

You can create a generic UIView and set its width or height to 1 pt in a storyboard. Set its backgroundColor to what you want the line to be. Make sure you set its constraints or resizing mask so that it doesn't grow in width/height when the screen is resized.

like image 188
Taum Avatar answered Nov 07 '22 19:11

Taum


 UIView *lineView = [[UIView alloc]init];
 lineView.frame = CGRectMake(0,50,self.View.frame.size.width,1);
 lineView.backgroundColor = [UIColor blackColor];
 [self.view addSubview:lineView];

Take this simple view created in code and just adjust its height by 1 or 2 Just like in the above code -> lineView.frame =CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>); CGFloat height is taken is 1

like image 25
Farooque Azam Avatar answered Nov 07 '22 18:11

Farooque Azam


The same exact thing. Use a UIView with a small width and set its background color to the desired color.

like image 4
ColdLogic Avatar answered Nov 07 '22 19:11

ColdLogic


// to draw line in uiview class

 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextSetLineWidth(context, 1.0);
 CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor]   CGColor]);
 CGContextMoveToPoint(context, xstartPoint, yStart);
 CGContextAddLineToPoint(context, xstartPoint, yBottom);
 CGContextStrokePath(context);
 CGContextSaveGState(context);
like image 1
Sukhmeet Hora Avatar answered Nov 07 '22 19:11

Sukhmeet Hora