我在为UICollectionViewCell
设置阴影路径时遇到问题,该路径的相对宽度为collectionView
范围。
我使用情节提要约束,在AwakeFromNib
方法中设置了阴影,并在sizeForItemAt
方法中调整了单元格的大小
//cell awakeFromNib
override func awakeFromNib() {
self.backgroundColor = UIColor.white
self.contentView.layer.cornerRadius = 2.0
self.contentView.layer.borderWidth = 1.0
self.contentView.layer.borderColor = UIColor.clear.cgColor
self.contentView.layer.masksToBounds = true
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOffset = CGSize(width: 0, height: 2.0)
self.layer.shadowRadius = 2.0
self.layer.shadowOpacity = 0.5
self.layer.masksToBounds = false
self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.contentView.layer.cornerRadius).cgPath
}
// collection view method
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let height: CGFloat = 288
return CGSize(width: collectionView.bounds.size.width-20, height: height)
}
单元格边界:
AwakeFromNib
方法-(307.0,288.0)
预期-(300.0,288.0)
出什么问题了?
答案 0 :(得分:0)
问题是,如果您的单元格布局,则边界/框架会改变。
将此添加到您的collectionViewCell-子类:
~ O(n log n)