【发布时间】: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