我正在尝试遮盖 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
}
正如您所看到的,我在这一行中添加了一个片段来尝试使角半径与屏幕尺寸(本质上是 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 被不正确地屏蔽:
知道如何解决吗?
谢谢:)
最佳答案
在您的cellForRowAtIndexPath
中,您正在使用自定义单元格,并在您的方法中给出角半径:
class func maskRowForIndexPath<T>(cell : T ,indexPath: NSIndexPath, count : Int) -> T
...您正在使用UITableViewCell
。尝试更改为您的 customCell
类。
关于ios - UIView Masking 无法与自动布局一起正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35003686/