gpt4 book ai didi

ios - UICollectioviewCell 中的 Uiview 的左下角和右下角半径问题

转载 作者:搜寻专家 更新时间:2023-11-01 06:11:31 26 4
gpt4 key购买 nike

UICollectionView Cell 出现问题。我试着用下面的代码只给 UIView 两个边角半径。我面临以下问题。任何建议都将不胜感激。谢谢

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

}

Issue image

Expectation Image

最佳答案

您可能想尝试以下代码来处理不同的 iOS 版本

  1. 添加UIView的扩展,您可以根据需要修改它。
extension UIView {
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
if #available(iOS 11.0, *) {
let cornerMasks = [
corners.contains(.topLeft) ? CACornerMask.layerMinXMinYCorner : nil,
corners.contains(.topRight) ? CACornerMask.layerMaxXMinYCorner : nil,
corners.contains(.bottomLeft) ? CACornerMask.layerMinXMaxYCorner : nil,
corners.contains(.bottomRight) ? CACornerMask.layerMaxXMaxYCorner : nil,
corners.contains(.allCorners) ? [CACornerMask.layerMinXMinYCorner, CACornerMask.layerMaxXMinYCorner, CACornerMask.layerMinXMaxYCorner, CACornerMask.layerMaxXMaxYCorner] : nil
].compactMap({ $0 })

var maskedCorners: CACornerMask = []
cornerMasks.forEach { (mask) in maskedCorners.insert(mask) }

self.clipsToBounds = true
self.layer.cornerRadius = radius
self.layer.maskedCorners = maskedCorners
} else {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
}
}
  1. 像这样使用函数:
cell.productnamevw.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 10)

还有一件事,对于您的情况,我建议您在单元格本身中设置单元格

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 - UICollectioviewCell 中的 Uiview 的左下角和右下角半径问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55823727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com