Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift add line above to control

In the below screen, I want to add horizontal line above to "Item" label and after "Add" button (below to the add button, I have dynamic table). UIGraphicsGetCurrentContext() doesn't work here.

Can someone help me how to do this?

Screenshot

like image 309
skumar Avatar asked May 07 '15 02:05

skumar


People also ask

How do you insert a line break in Swift?

If you want to use line breaks to make your source code easier to read, but you don't want the line breaks to be part of the string's value, write a backslash ( \ ) at the end of those lines: let softWrappedQuotation = """ The White Rabbit put on his spectacles.

How do I make a circle in Swift?

With Border Color and Border Size and the default Background property you can define the appearance of the circle. Please note, to draw a circle the view's height and width have to be equal in size. The code is working for Swift >= 4 and Xcode >= 9 .


3 Answers

It is simple to add a subview to act as a line. For example:

Swift 4

var lineView = UIView(frame: CGRect(x: 0, y: 100, width: 320, height: 1.0))
lineView.layer.borderWidth = 1.0
lineView.layer.borderColor = UIColor.black.cgColor
self.view.addSubview(lineView)

Objective C

UIView * lineview = [[UIView alloc] initWithFrame:CGRectMake(0, 100,320,1)];
lineview.layer.borderColor = [UIColor blackColor].CGColor;
lineview.layer.borderWidth = 1.0;
[self.view addSubview:lineview];

Or you can refer to this link to add CALayer or draw a view

how do you draw a line programmatically from a view controller?

like image 103
Leo Avatar answered Oct 23 '22 17:10

Leo


You can achieve this in Storyboard.

All you have to do is drag a UIView with height = 1 and width whatever is good for you (ideally equal to screen width). Place it where you want the lines to be.

like image 25
Vivek Molkar Avatar answered Oct 23 '22 16:10

Vivek Molkar


For Swift 3:

        let lineView = UIView(frame: CGRect(x:0,
                                            y:  self.yourLabel.height - 1 ,
                                            width: self.yourLabel.frame.width,
                                            height: 1.4
                                           )
                             )

        lineView.backgroundColor = UIColor.blue
        self.yourLabel.addSubview(lineView)
like image 42
Hamed Avatar answered Oct 23 '22 17:10

Hamed