【问题标题】:UIView Masking not working correctly with autolayoutUIView Masking 无法与自动布局一起正常工作
【发布时间】:2016-05-02 10:00:06
【问题描述】:

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

通过以下代码实现:

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
    }

如您所见,我添加了一个 sn-p 来尝试使拐角半径与此行中的屏幕尺寸(本质上是视图尺寸)成比例:

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

你知道怎么解决吗?

谢谢:)

【问题讨论】:

  • 将遮罩应用到 UITableView 怎么样?还是这些部分?另外,您的故事板的大小是多少(Any-Any 或其他)?
  • 将掩码应用于 tableview 不会得到适当的结果 - 例如在有多个部分的情况下(如提供的图像中所示)。这是一个 tableview,所以如果我将 tableview 的角弄圆,第一部分的底角不会像第二部分的顶角一样被倒圆。我没有使用尺码等级,因为它严格来说是一个 iPhone 应用程序。故事板推断的视图控制器大小为 4.7 英寸(iPhone 6)。 @vrwim
  • 您是否尝试过在 tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) 而不是在 cellForRowAtIndexPath 中应用掩码?

标签: ios xcode swift uitableview autolayout


【解决方案1】:

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

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

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

【讨论】:

  • 不知道这有什么关系,因为我的自定义单元格是 UITableViewCell 的子类 - 参数类只是它适用于所有 UITableViewCells
  • 虽然您的 customcell 是 UITableviewCell 的子类,但您必须提及您的 customcell 而不是您的 UITableViewCell,因为默认情况下 UITableView 将只管理 UITableViewCell 而不是您的 customCell。因此,在您的方法中更改为您的 customCell 。这将解决您的问题
猜你喜欢
  • 1970-01-01
  • 2015-07-13
  • 1970-01-01
  • 2015-04-23
  • 2015-07-06
  • 1970-01-01
  • 2014-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多