簡體   English   中英

UITableViewCell 左右邊距

[英]UITableViewCell margin left and right

我想制作這樣的細胞。 我用迅捷。

但我不知道如何在單元格的左右兩側添加邊距。

你知道如何在截面上做同樣的邊緣效果嗎? (當有多個單元格時,接觸的邊緣不圓角) 在此處輸入圖片說明

當我使用 contentInset 時:

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

在此處輸入圖片說明

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

在此處輸入圖片說明

從您的屏幕截圖來看,您似乎正在嘗試使用分組表視圖來執行此操作。 要做到這一點,你應該使用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子類。

你應該得到如下結果:

在此處輸入圖片說明

從 iOS 13.0 開始,有一個新的 tableview 樣式“Inset grouped”可以做到這一點: https : //developer.apple.com/documentation/uikit/uitableview/style/insetgrouped

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM