【问题标题】:UITableViewCell margin left and rightUITableViewCell 左右边距
【发布时间】:2016-12-07 18:20:32
【问题描述】:

我想做这样的细胞。我用 swift。

但是我不知道如何在单元格的左右两边添加边距。

你知道如何在截面上做同样的边缘效果吗? (当有多个单元格时,接触的边缘不圆角)

当我使用 contentInset 时:

self.tableView.contentInset = UIEdgeInsetsMake(0, -15, 0, 0)

self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, -15)

【问题讨论】:

  • 试试这个self.tableView.contentInset = UIEdgeInsetsMake(0, -15, 0, -15)
  • 右边有效,左边无效...
  • 自定义坐标,肯定有效
  • self.tableView.contentInset = UIEdgeInsetsMake(, , , )
  • 放左边距的时候是右边。

标签: ios swift uitableview margin


【解决方案1】:

从您的屏幕截图看来,您正在尝试使用分组表视图来执行此操作。为此,您应该使用 UITableView 添加到 UIViewController 而不是 UITableViewController

要设置插图,您只需将表格视图的约束/框架设置为从左右边缘略微向内,并将视图的背景颜色设置为UIColor.groupTableViewBackgroundColor()

然后在cellForRowAtIndexPath 你可以这样说:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cornerRadius:CGFloat = 5.0

        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

        // Configure your cell

        let sectionCount = tableView.numberOfRowsInSection(indexPath.section)
        let shapeLayer = CAShapeLayer()
        cell.layer.mask = nil
        if sectionCount > 1
        {

            switch indexPath.row {
            case 0:
                var bounds = cell.bounds
                bounds.origin.y += 1.0
                let bezierPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: [.TopLeft, .TopRight], cornerRadii: CGSize(width: cornerRadius,height: cornerRadius))
                shapeLayer.path = bezierPath.CGPath
                cell.layer.mask = shapeLayer
            case sectionCount - 1:
                var bounds = cell.bounds
                bounds.size.height -= 1.0
                let bezierPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: [.BottomLeft, .BottomRight], cornerRadii: CGSize(width: cornerRadius,height: cornerRadius))
                shapeLayer.path = bezierPath.CGPath
                cell.layer.mask = shapeLayer
            default:
                break
            }
            return cell
        }
        else
        {
            let bezierPath = UIBezierPath(roundedRect: CGRectInset(cell.bounds,0.0,2.0), cornerRadius: cornerRadius)
            shapeLayer.path = bezierPath.CGPath
            cell.layer.mask = shapeLayer
            return cell
        }
    }

您只需根据行的索引路径和节中的行数应用掩码。如果您有动态大小的单元格,您可能需要将掩码应用到您的 UITableViewCell 子类。

你应该得到如下结果:

【讨论】:

    【解决方案2】:

    从 iOS 13.0 开始,有一个新的 tableview 样式“Inset grouped”可以做到这一点: https://developer.apple.com/documentation/uikit/uitableview/style/insetgrouped

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-29
      • 2020-09-22
      • 2017-11-21
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      • 2017-12-06
      相关资源
      最近更新 更多