【发布时间】:2017-02-06 09:26:03
【问题描述】:
我的牢房里有UIView。 UICollectionview 第一次加载,阴影看起来不像我想要的那样。但是当我滚动时,阴影变得正常。
self.backView.layer.cornerRadius = 3
self.backView.clipsToBounds = true
self.backView.layer.shadowPath = UIBezierPath(roundedRect: self.backView.bounds, cornerRadius: self.backView.layer.cornerRadius).CGPath
self.backView.layer.shadowColor = UIColor.blackColor().CGColor
self.backView.layer.shadowOpacity = 0.3
self.backView.layer.shadowOffset = CGSizeMake(0, 1)
self.backView.layer.shadowRadius = 1
self.backView.layer.masksToBounds = false
阴影渲染错误:
阴影渲染正确:
Edit-1:我找到了根本原因,但没有找到解决方案。故事板的单元格框架和运行时的单元格框架不同,因为我像这样更改单元格的高度和宽度:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(collectionView.frame.width/3, collectionView.frame.height)
}
因此,当我删除此代码时,阴影会正确显示。但我无法通过代码设置宽度和高度。
【问题讨论】:
-
你在哪里运行的代码?
-
collectionViewCell 内
-
显示一些额外的代码
-
在你的 cellForItemAtIndexPath 添加 cell.layoutIfNeeded() 之前返回单元格