【发布时间】:2019-04-24 06:34:46
【问题描述】:
我遇到了 UICollectionView Cell 的一个问题。我试图用下面的代码只给UIView 2 个边角半径。我面临以下问题。任何建议都会不胜感激。谢谢你
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell : FeaturedCell = collectionView.dequeueReusableCell(withReuseIdentifier: "FeaturedCell", for: indexPath) as! FeaturedCell
let objModel = arrSearch[indexPath.row]
cell.lblproducttitle.text = "\(objModel.Name!) \n$\(objModel.price!)"
cell.imgproduct.sd_setImage(with: URL(string: objModel.imgUrl), placeholderImage: UIImage(named: "logo"), options: .refreshCached, completed: nil)
let mask = CAShapeLayer()
mask.bounds = cell.productnamevw.frame
mask.position = cell.productnamevw.center
mask.backgroundColor = cell.productnamevw.backgroundColor?.cgColor
let path = UIBezierPath(roundedRect: cell.productnamevw.bounds, byRoundingCorners: [.bottomLeft , .bottomRight], cornerRadii: CGSize(width: 10, height: 10))
mask.path = path.cgPath
cell.productnamevw.layer.mask = mask
cell.productnamevw.layer.masksToBounds = true
return cell
}
【问题讨论】:
-
@HimanshuMoradiya 上 2 个角是如何圆角的?添加您的视图层次结构及其顺序。
-
尝试使用
cell.contentView而不仅仅是cell -
你的 iOS 版本是多少?
-
@Himanshu Moradiya 您不需要为您的子视图制作角半径(即灰色)。您只需将容器视图的“clipbounds”设置为 true。仅当容器的角与子视图的角相同时才执行此操作。
-
这可能与我描述的in this answer 和in this answer 相同的问题:当您创建遮罩路径时,视图(在本例中为
cell.productnamevw)尚未设置为其最终大小,所以cell.productnamevw.bounds的大小错误。但是,我没有将此问题标记为重复问题,因为在我写完这些答案后,iOS 11 将maskedCorners属性添加到CALayer。设置maskedCorners(正如这里的几个答案所建议的那样)是一个更简单的解决方案。
标签: ios swift uikit uicollectionviewcell uibezierpath