Ios UICollectioviewCell内Uiview的左下角和右下角半径存在问题
我在Ios UICollectioviewCell内Uiview的左下角和右下角半径存在问题,ios,swift,uikit,uicollectionviewcell,uibezierpath,Ios,Swift,Uikit,Uicollectionviewcell,Uibezierpath,我在UICollectionViewCell上遇到了一个问题。我试图给UIView提供2个边角半径,代码如下。我面临以下问题。任何建议都将不胜感激。多谢各位 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell : FeaturedCell = collectionView.d
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
}
请注意,这仅适用于iOS 11.0+
yourview.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner, .layerMinXMinYCorner]
请注意,这仅适用于iOS 11.0+
yourview.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner, .layerMinXMinYCorner]
您可能需要尝试以下代码来处理不同的iOS版本
class FeaturedCell: UICollectionViewCell {
@IBOutlet private weak var productnamevw: UIView!
override func awakeFromNib() {
super.awakeFromNib()
self.productnamevw.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 10)
}
override func layoutSubviews() {
super.layoutSubviews()
self.productnamevw.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 10)
}
}
您可能需要尝试以下代码来处理不同的iOS版本
class FeaturedCell: UICollectionViewCell {
@IBOutlet private weak var productnamevw: UIView!
override func awakeFromNib() {
super.awakeFromNib()
self.productnamevw.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 10)
}
override func layoutSubviews() {
super.layoutSubviews()
self.productnamevw.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 10)
}
}
对于Swift 4+和iOS 11.0更高版本,这将起作用:
- For top left corner = [.layerMinXMinYCorner]
- For top right corner = [.layerMaxXMinYCorner]
- For bottom left corner = [.layerMinXMaxYCorner]
- For bottom right corner = [.layerMaxXMaxYCorner]
根据这一点,您的代码应该如下所示:
cell.productnamevw.layer.cornerRadius = 10 //or whatever you want to give
cell.productnamevw.layer.maskedCorners = [. layerMinXMaxYCorner, . layerMaxXMaxYCorner]
对于Swift 4+和iOS 11.0更高版本,这将起作用:
- For top left corner = [.layerMinXMinYCorner]
- For top right corner = [.layerMaxXMinYCorner]
- For bottom left corner = [.layerMinXMaxYCorner]
- For bottom right corner = [.layerMaxXMaxYCorner]
根据这一点,您的代码应该如下所示:
cell.productnamevw.layer.cornerRadius = 10 //or whatever you want to give
cell.productnamevw.layer.maskedCorners = [. layerMinXMaxYCorner, . layerMaxXMaxYCorner]
@Himanshuradia上2个角是如何圆角的?添加视图层次结构及其顺序。尝试使用
cell.contentView
而不仅仅是cell
您的iOS版本是什么?@Himanshu Moradiya您不需要为子视图创建圆角半径(即灰色)。您只需将容器视图的“剪贴簿”设置为true。仅当容器的角点与子视图的角点相同时才执行此操作。这可能与我描述的问题相同:在创建掩码路径时,视图(cell.productnamevw
)尚未设置为其最终大小,因此cell.productnamevw.bounds
的大小错误。但是,我没有将这个问题标记为重复问题,因为在我写下这些答案后,iOS 11将maskedCorners
属性添加到CALayer
。设置遮罩角是一个更简单的解决方案。@himanshuradiya上二角的圆角是多少?添加视图层次结构及其顺序。尝试使用cell.contentView
而不仅仅是cell
您的iOS版本是什么?@Himanshu Moradiya您不需要为子视图创建圆角半径(即灰色)。您只需将容器视图的“剪贴簿”设置为true。仅当容器的角点与子视图的角点相同时才执行此操作。这可能与我描述的问题相同:在创建掩码路径时,视图(cell.productnamevw
)尚未设置为其最终大小,因此cell.productnamevw.bounds
的大小错误。但是,我没有将这个问题标记为重复问题,因为在我写下这些答案后,iOS 11将maskedCorners
属性添加到CALayer
。设置maskedCorners
(正如这里的几个答案所建议的那样)是一个更简单的解决方案。这不起作用,因为调用awakeFromNib
时视图尚未设置为其最终大小。您需要在productnamevw
的layoutSubviews
方法中计算掩码路径。您的答案是正确的,但当我第一次加载时,它会显示一半结果,当我向下滚动并返回顶部时,它会工作well@HimanshuMoradiya是的,正如robmayoff所说,当从NIB唤醒时,视图还没有设置为最终大小。我更新了代码,希望有以下帮助:)这将不起作用,因为调用awakeFromNib
时视图尚未设置为其最终大小。您需要在productnamevw
的layoutSubviews
方法中计算掩码路径。您的答案是正确的,但当我第一次加载时,它会显示一半结果,当我向下滚动并返回顶部时,它会工作well@HimanshuMoradiya是的,正如robmayoff所说,当从NIB唤醒时,视图还没有设置为最终大小。我更新了代码,希望这有帮助:)