问题
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?
回答1:
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
回答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.
回答3:
Objective-C
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
In Swift
tableView.separatorStyle = .None
回答4:
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
.
来源:https://stackoverflow.com/questions/40298946/trying-to-draw-dashed-border-for-uitableviewcell