【问题标题】:Trying to draw dashed border for UITableViewCell尝试为 UITableViewCell 绘制虚线边框
【发布时间】:2016-10-28 06:03:25
【问题描述】:

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

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)
}

但是,我的虚线后面出现了一条奇怪的实线,如下所示:http://imgur.com/6kR9PgZ

我已经在viewDidLoad中设置了tableView.separatorColor = UIColor.clear

任何想法为什么我在虚线后面得到那条实线?

【问题讨论】:

    标签: ios swift uitableview swift3 cashapelayer


    【解决方案1】:

    试试这个

    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)
    }
    

    输出

    【讨论】:

    • 请告诉我们应该从哪里调用这个函数。如果每个单元格的路径不同会怎样
    【解决方案2】:

    如果你写过

    tableView.separatorStyle = .None

    不过,你在 dassed 模式下方得到一条实线,然后 100% 它不是 tableview 分隔符。这是另一回事

    检查您的表格背景颜色,当您清除分隔符颜色时, 单元格之间留有小空间。如果您将单元格背景颜色 与表格背景颜色不同的颜色,它像 分隔符。

    【讨论】:

      【解决方案3】:

      目标-C

      self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
      

      在斯威夫特中

      tableView.separatorStyle = .None
      

      【讨论】:

      • 不会有任何区别。问题在于层逻辑
      【解决方案4】:

      你用donted线显示的那一行是默认的TableViewCell分隔符,你可以直接从界面构建器中删除它,而不用编写任何代码。

      在界面生成器中选择您的TableView 并在Attributes Insepector 中将Separator 属性设置为None。

      【讨论】:

        猜你喜欢
        • 2017-07-19
        • 1970-01-01
        • 1970-01-01
        • 2023-01-12
        • 2014-03-22
        • 1970-01-01
        • 2012-06-04
        • 2014-07-18
        • 1970-01-01
        相关资源
        最近更新 更多