Попытка добавить пунктирный разделитель границ в табличном представлении - PullRequest
0 голосов
/ 13 мая 2018

Используя этот пример, я пытаюсь добавить пунктирную границу к моему UITableView.

Пытаться нарисовать пунктирную границу для UITableViewCell

Но это не работает.Он ничего не показывает.

func addDashedBottomBorder(to cell: UITableViewCell) {
    let color = UIColor.black.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)
}

Я использую этот метод в cellForRowAt, но он ничего не показывает и в viewDidLoad, table.separatorStyle = .none.

1 Ответ

0 голосов
/ 13 мая 2018

Из следующих двух строк вашего кода:

    shapeLayer.lineJoin = kCALineJoinRound
    shapeLayer.path = UIBezierPath(roundedRect: CGRect(x: 0, y: shapeRect.height, width: shapeRect.width, height: 0), cornerRadius: 0).cgPath

kCALineJoinRound создает верхнюю и нижнюю штриховые линии, но они перекрываются, поскольку height из UIBezierPath равно 0. Итак,Обновлен ваш код как:

    shapeLayer.path = UIBezierPath(roundedRect: CGRect(x: 0, y: shapeRect.height, width: shapeRect.width, height: 1), cornerRadius: 0).cgPath

Он скроет нижнюю строку и вы получите желаемый результат.

Лучшее решение:

Скореечем скрыть нижнюю черту, вы можете исправить ее, указав CAShapeLayer просто *1017*, например:

    shapeLayer.lineJoin = kCALineJoinRound
    shapeLayer.lineDashPhase = 3.0 // Add "lineDashPhase" property to CAShapeLayer
    shapeLayer.lineDashPattern = [9,6]
    shapeLayer.path = UIBezierPath(roundedRect: CGRect(x: 0, y: shapeRect.height, width: shapeRect.width, height: 0), cornerRadius: 0).cgPath

Полученная строка:

enter image description here

...