ios - UIView Masking 无法与自动布局一起正常工作

标签 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
    }

正如您所看到的,我在这一行中添加了一个片段来尝试使角半径与屏幕尺寸(本质上是 View 尺寸)成比例:

    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)屏幕上运行该应用程序时, View 被不正确地屏蔽:

enter image description here

知道如何解决吗?

谢谢:)

最佳答案

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

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

...您正在使用UITableViewCell。尝试更改为您的 customCell 类。

关于ios - UIView Masking 无法与自动布局一起正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35003686/

相关文章:

swift - 为什么我仍然需要解开 Swift 字典值?

ios - Realm 对象中的两个相互关联的关系

iphone - iOS 崩溃并出现 EXC_BAD_ACCESS(code=1)

ios - 奇怪且令人困惑的 Xcode 断点

swift - 在函数中捕获 swift 闭包值

swift - 我需要使用线程类捕获 self 吗?

ios - 如何让我的 iOS7 UITableViewController 不出现在顶部状态栏下?

ios - IQKeyboardManager 的“完成”按钮操作在 Swift 中不起作用

ios - 在不使用 aws 的情况下为 RestKit 生成 S3 bucket url

iphone - 将图像存储在数组中