6

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

Vivek Molkar
  • 3,910
  • 1
  • 34
  • 46
skumar
  • 985
  • 4
  • 14
  • 37

3 Answers3

14

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?

Leo
  • 24,596
  • 11
  • 71
  • 92
12

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.

Vivek Molkar
  • 3,910
  • 1
  • 34
  • 46
4

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)
Hamed
  • 1,678
  • 18
  • 30