添加圆angular和投影到UICollectionViewCell

所以我已经通过添加第二个视图添加阴影的各种职位,但我仍然无法得到它的工作,如果我想将其添加到UICollectionViewCell 。 我subclassed UICollectionViewCell ,这里是我的代码,我添加各种UI元素的单元格的内容视图,并添加阴影到图层:

 [self.contentView setBackgroundColor:[UIColor whiteColor]]; self.layer.masksToBounds = NO; self.layer.shadowOffset = CGSizeMake(0, 1); self.layer.shadowRadius = 1.0; self.layer.shadowColor = [UIColor blackColor].CGColor; self.layer.shadowOpacity = 0.5; [self.layer setShadowPath:[[UIBezierPath bezierPathWithRect:self.bounds] CGPath]]; 

我想知道如何添加圆angular和阴影到UICollectionViewCell

这些解决scheme都不适合我。 如果将所有子视图放置在UICollectionViewCell内容视图中,则可以在单元图层上设置阴影,并在contentView的图层上设置边框以获得两个结果。

 cell.contentView.layer.cornerRadius = 2.0f; cell.contentView.layer.borderWidth = 1.0f; cell.contentView.layer.borderColor = [UIColor clearColor].CGColor; cell.contentView.layer.masksToBounds = YES; cell.layer.shadowColor = [UIColor lightGrayColor].CGColor; cell.layer.shadowOffset = CGSizeMake(0, 2.0f); cell.layer.shadowRadius = 2.0f; cell.layer.shadowOpacity = 1.0f; cell.layer.masksToBounds = NO; cell.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds cornerRadius:cell.contentView.layer.cornerRadius].CGPath; 

Swift 3.0

 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.lightGray.cgColor self.layer.shadowOffset = CGSize(width: 0, height: 2.0) self.layer.shadowRadius = 2.0 self.layer.shadowOpacity = 1.0 self.layer.masksToBounds = false self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.contentView.layer.cornerRadius).cgPath 

Swift 3版本:

  cell.contentView.layer.cornerRadius = 10 cell.contentView.layer.borderWidth = 1.0 cell.contentView.layer.borderColor = UIColor.clear.cgColor cell.contentView.layer.masksToBounds = true cell.layer.shadowColor = UIColor.gray.cgColor cell.layer.shadowOffset = CGSize(width: 0, height: 2.0) cell.layer.shadowRadius = 2.0 cell.layer.shadowOpacity = 1.0 cell.layer.masksToBounds = false cell.layer.shadowPath = UIBezierPath(roundedRect:cell.bounds, cornerRadius:cell.contentView.layer.cornerRadius).cgPath 

如果有帮助:下面是四舍五入的快捷方式:

  cell.layer.cornerRadius = 10 cell.layer.masksToBounds = true 

与单元格是一个variables控制单元格:通常,您将使用此override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell

请享用!

设置单元格的layer属性,而不是contentView

 CALayer * layer = [cell layer]; [layer setShadowOffset:CGSizeMake(0, 2)]; [layer setShadowRadius:1.0]; [layer setShadowColor:[UIColor redColor].CGColor] ; [layer setShadowOpacity:0.5]; [layer setShadowPath:[[UIBezierPath bezierPathWithRect:cell.bounds] CGPath]]; 

您只需(a)设置cornerRadius和(b)将shadowPath设置为与cornerRadius具有相同半径的四舍五入矩形:

 self.layer.cornerRadius = 10; self.layer.shadowPath = [[UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:self.layer.cornerRadius] CGPath]; 

我不得不为swift做一些小的改动:

  cell.contentView.layer.cornerRadius = 2.0; cell.contentView.layer.borderWidth = 1.0; cell.contentView.layer.borderColor = UIColor.clearColor().CGColor; cell.contentView.layer.masksToBounds = true; cell.layer.shadowColor = UIColor.grayColor().CGColor; cell.layer.shadowOffset = CGSizeMake(0, 2.0); cell.layer.shadowRadius = 2.0; cell.layer.shadowOpacity = 1.0; cell.layer.masksToBounds = false; cell.layer.shadowPath = UIBezierPath(roundedRect:cell.bounds, cornerRadius:cell.contentView.layer.cornerRadius).CGPath; 

这个为我工作

  cell.contentView.layer.cornerRadius = 5.0 cell.contentView.layer.borderColor = UIColor.gray.withAlphaComponent(0.5).cgColor cell.contentView.layer.borderWidth = 0.5 let border = CALayer() let width = CGFloat(2.0) border.borderColor = UIColor.darkGray.cgColor border.frame = CGRect(x: 0, y: cell.contentView.frame.size.height - width, width: cell.contentView.frame.size.width, height: cell.contentView.frame.size.height) border.borderWidth = width cell.contentView.layer.addSublayer(border) cell.contentView.layer.masksToBounds = true cell.contentView.clipsToBounds = true