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.
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.
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
The same exact thing. Use a UIView
with a small width and set its background color to the desired color.
// 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With