我想了解为什么将 clipsToBounds / masksToBounds 设置为 False 对UICollectionViewCell子视图(在我的情况下是UILabel)没有影响。
我尝试使用 clipsToBounds / masksToBounds 进行任何可能的解决方案(尽管我认为它们的行为/做同样的事情)。
override open func layoutSubviews() {
super.layoutSubviews()
super.clipsToBounds = false
super.layer.masksToBounds = false
self.clipsToBounds = false
self.layer.masksToBounds = false
self.contentView.clipsToBounds = false
self.contentView.layer.masksToBounds = false
self.contentView.superview?.clipsToBounds = false
self.contentView.superview?.layer.masksToBounds = false
self.endTime.layer.masksToBounds = false
self.endTime.clipsToBounds = false
self.endTime.layer.zPosition = CGFloat(FLT_MAX)
}
我根据Custom layout from raywenderlich覆盖 UICollectionViewLayout
override public func prepare() {
if cache.isEmpty {
let columnWidth = contentWidth / CGFloat(numberOfColumns)
var xOffset = [CGFloat]()
for column in 0 ..< numberOfColumns {
xOffset.append(CGFloat(column) * columnWidth )
}
let column = 0
var yOffset = [CGFloat](repeating: 0, count: numberOfColumns)
for item in 0 ..< collectionView!.numberOfItems(inSection: 0) {
let indexPath = NSIndexPath(row: item, section:0)
let frame = CGRect(x: xOffset[column], y: yOffset[column], width: cellSize.width, height: cellSize.height)
let insetFrame = frame.insetBy(dx: cellPaddingX, dy: cellPaddingY)
let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath as IndexPath)
attributes.frame = insetFrame
cache.append(attributes)
contentHeight = max(contentHeight, frame.maxY)
yOffset[column] = yOffset[column] + cellSize.height + minimumLineSpacing
}
}
}
public override var collectionViewContentSize: CGSize {
return CGSize(width: contentWidth, height: contentHeight)
}
override open func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var layoutAttributes = [UICollectionViewLayoutAttributes]()
for attributes in cache {
if attributes.frame.intersects(rect) {
layoutAttributes.append(attributes)
}
}
return layoutAttributes
}
结果:
红色垂直线表示“不可用”,它从一个单元格开始,可能在另一个单元格中完成(取决于持续时间)。 从UI Debug看来,每个下一个单元格总是在前一个单元格的顶部。(21:00单元格是唯一看起来像我想要的单元格)