本文介绍了试图为 UITableViewCell 绘制虚线边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码为 UITableViewCells 绘制虚线底部边框:

func addDashedBottomBorder(to cell: UITableViewCell) {让宽度 = CGFloat(2.0)让 dashedBorderLayer: CAShapeLayer = CAShapeLayer()让 frameSize = cell.frame.size让 shapeRect = CGRect(x: 0, y: frameSize.height, width: frameSize.width*2, height: 1)dashedBorderLayer.bounds = shapeRectdashedBorderLayer.position = CGPoint(x: 0, y: frameSize.height)dashedBorderLayer.strokeColor = UIColor.lightGray.cgColordashedBorderLayer.lineWidth = 宽度dashedBorderLayer.lineDashPattern = [9, 6]dashedBorderLayer.path = UIBezierPath(roundedRect: shapeRect, cornerRadius: 5).cgPathcell.layer.addSublayer(dashedBorderLayer)}

但是,我的虚线后面出现了一条奇怪的实线,如下所示:

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: http://imgur.com/6kR9PgZ

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

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

解决方案

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

这篇关于试图为 UITableViewCell 绘制虚线边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 00:30