UIView Masking与autolayout无法正常工作

时间:2016-01-25 22:31:03

标签: ios xcode swift uitableview autolayout

我试图掩盖UITableView的底角和顶角。我发现以下代码适用于特定的屏幕尺寸(iPhone 6)并产生以下结果:

enter image description here

通过应用以下代码实现:

class func maskRowForIndexPath<T>(cell : T ,indexPath: NSIndexPath, count : Int) -> T{
        let const = UIScreen.mainScreen().bounds.width * (6 / (cell as! UIView).frame.size.width)
        let cornerRadii = CGSizeMake(const, const)
        let row = indexPath.row
        if row == 0 {
            let maskLayer = CAShapeLayer()
            maskLayer.path = UIBezierPath(roundedRect: (cell as! UITableViewCell).bounds, byRoundingCorners: UIRectCorner.TopLeft.union(.TopRight), cornerRadii: cornerRadii).CGPath
            (cell as! UITableViewCell).layer.mask = maskLayer
        }

        if row == count-1{
            let maskLayer = CAShapeLayer()
            maskLayer.path = UIBezierPath(roundedRect: (cell as! UITableViewCell).bounds, byRoundingCorners: UIRectCorner.BottomLeft.union(.BottomRight), cornerRadii: cornerRadii).CGPath
            (cell as! UITableViewCell).layer.mask = maskLayer
        }
        return cell
    }

正如您所看到的,我添加了一个片段,尝试在此行中使角半径与屏幕大小(以及视图大小)成比例:

    let const = UIScreen.mainScreen().bounds.width * (6 / (cell as! UIView).frame.size.width)

maskRowForIndexPath方法在我的UITableView cellForRowAtIndexPath方法中调用:

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

        let row = indexPath.row
        let info = (indexPath.section == 0) ? firstSectionTitles : secondSectionTitles
        let cell : AccountHomeTableViewCell = tableView.dequeueReusableCellWithIdentifier("AccountHomeTableViewCell") as! AccountHomeTableViewCell
        cell.titleLabel.attributedText = attributedTitleForCellString(info[row].title)
        cell.cellImageView.image = UIImage(named: info[row].imageName)
        cell.layoutMargins = UIEdgeInsetsZero;
        UIView.maskRowForIndexPath(cell, indexPath: indexPath, count: info.count)

        return cell
    }

当我在iPhone 6+(和较小的iPhone 5)屏幕上运行应用程序时,视图被屏蔽不正确:

enter image description here

关于如何解决的任何想法?

谢谢:)

1 个答案:

答案 0 :(得分:0)

cellForRowAtIndexPath您正在使用自定义单元格,并在方法中给出角半径:

class func maskRowForIndexPath<T>(cell : T ,indexPath: NSIndexPath, count : Int) -> T

...您正在使用UITableViewCell。请尝试更改为customCell课程。