0

I'm trying to draw a dashed bottom border to UITableViewCells with the following code:

func addDashedBottomBorder(to cell: UITableViewCell) {
    let width = CGFloat(2.0)
    let dashedBorderLayer: CAShapeLayer = CAShapeLayer()
    let frameSize = cell.frame.size
    let shapeRect = CGRect(x: 0, y: frameSize.height, width: frameSize.width*2, height: 1) 

    dashedBorderLayer.bounds = shapeRect
    dashedBorderLayer.position = CGPoint(x: 0, y: frameSize.height)
    dashedBorderLayer.strokeColor = UIColor.lightGray.cgColor
    dashedBorderLayer.lineWidth = width
    dashedBorderLayer.lineDashPattern = [9, 6]
    dashedBorderLayer.path = UIBezierPath(roundedRect: shapeRect, cornerRadius: 5).cgPath

    cell.layer.addSublayer(dashedBorderLayer)
}

However, I'm getting a strange solid line behind my dashed line as can be seen here: https://i.stack.imgur.com/eM3Rs.jpg

I've already set tableView.separatorColor = UIColor.clear in viewDidLoad

Any ideas why I'm getting that solid line behind the dashed one?

Nirav D
  • 71,513
  • 12
  • 161
  • 183

4 Answers4

3

Try this

func addDashedBottomBorder(to cell: UITableViewCell) {

    let color = UIColor.lightGray.cgColor

    let shapeLayer:CAShapeLayer = CAShapeLayer()
    let frameSize = cell.frame.size
    let shapeRect = CGRect(x: 0, y: 0, width: frameSize.width, height: 0)

    shapeLayer.bounds = shapeRect
    shapeLayer.position = CGPoint(x: frameSize.width/2, y: frameSize.height)
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.strokeColor = color
    shapeLayer.lineWidth = 2.0
    shapeLayer.lineJoin = kCALineJoinRound
    shapeLayer.lineDashPattern = [9,6]
    shapeLayer.path = UIBezierPath(roundedRect: CGRect(x: 0, y: shapeRect.height, width: shapeRect.width, height: 0), cornerRadius: 0).cgPath

    cell.layer.addSublayer(shapeLayer)
}

Output

enter image description here

Rajan Maheshwari
  • 14,465
  • 6
  • 64
  • 98
2

if you have write

tableView.separatorStyle = .None

Still, yo are getting a solid line below dassed pattern then 100% it is not tableview separator. It is something else

check your Table background colour, When you clear separator color, a small space remains between cell. If you cell background color different color from table background color, it visible like a separator.

Mahendra Y
  • 1,941
  • 20
  • 26
1

Objective-C

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

In Swift

tableView.separatorStyle = .None
Vinodh
  • 5,262
  • 4
  • 38
  • 68
smoothdvd
  • 2,389
  • 4
  • 20
  • 25
1

The line you are showing with your donted line is default TableViewCell seperator, you can remove it directly from the interface builder instead of writing any code.

In interface builder select your TableView and in the Attributes Insepector Set Separator property to None.

enter image description here

Nirav D
  • 71,513
  • 12
  • 161
  • 183