gpt4 book ai didi

ios - subview 未添加某些 UICollectionViewCells 和闪烁(以编程方式)

转载 作者:行者123 更新时间:2023-12-04 08:07:28 24 4
gpt4 key购买 nike

我正在尝试使用 Collection View 制作自定义图像 slider 。我希望它可以重复使用。所以我制作了单独的自定义类,其中所有 collectionView 的东西。然后从 UIViewController 调用该类,如下面的代码所示。而我的 UIColletonViewCell 只包含 imageView。
问题是在第二个单元格中。 imageView 没有被添加,在第三个单元格上,它也会闪烁。我试图调试这些问题,但不能。
ImageSlider 类和 UICollectionViewCell 类在末端,带有 Collection View 的东西:

class ImageSlider: UIView {
var imgArr = [UIImage(named: "one.jpg"), UIImage(named: "3.jpg"), UIImage(named: "two.jpg"), UIImage(named: "4.jpg")]
var sliderCollection : UICollectionView = {
let widthRatio : Float = 16.0
let heightRatio : Float = 9.0
let collecionWidth = UIScreen.main.bounds.width - 30
let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: CGRect(x: 15, y: 100, width: collecionWidth, height: CGFloat((Float(collecionWidth)*heightRatio)/widthRatio)), collectionViewLayout: layout)
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
collectionView.backgroundColor = .systemOrange
collectionView.isPagingEnabled = true
// collectionView.isScrollEnabled = true
collectionView.register(ImageSliderCell.self, forCellWithReuseIdentifier: "ImageSliderCell")
return collectionView
}()


}

extension ImageSlider: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imgArr.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageSliderCell", for: indexPath) as! ImageSliderCell

cell.imgView.image = imgArr[indexPath.item]
// cell.imgView.contentMode = .scaleAspectFit
print("cell frame : ", "(\(cell.frame.width), \(cell.frame.height)")
print("imgView frame : ", "(\(cell.imgView.frame.width), \(cell.imgView.frame.height)")
return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
}

}

class ImageSliderCell: UICollectionViewCell {
var imgView = UIImageView()

// override func awakeFromNib() {
// self.addSubview(imgView)
// }

override init(frame: CGRect) {
super.init(frame: frame)
imgView.frame = frame
self.addSubview(imgView)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
这是 ViewController,我在其中调用 ImageSlider() 类。
class ImageSliderVC: UIViewController, UICollectionViewDelegate {

let imageSlider = ImageSlider()

override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(imageSlider.sliderCollection)
imageSlider.sliderCollection.delegate = imageSlider
imageSlider.sliderCollection.dataSource = imageSlider
imageSlider.sliderCollection.reloadData()
}
}

最佳答案

看起来它在没有约束的情况下无法工作,因为 UICollectionViewCell 可以用零帧创建,并将其转换为单元格内的 imageView。您需要向 imageView 添加约束以使其可见。

extension UIView {

func centerX(inView view: UIView, constant: CGFloat = 0) {
translatesAutoresizingMaskIntoConstraints = false
centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: constant).isActive = true
}

func centerY(inView view: UIView, constant: CGFloat = 0) {
translatesAutoresizingMaskIntoConstraints = false
centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: constant).isActive = true
}

func setDimensions(height: CGFloat, width: CGFloat) {
translatesAutoresizingMaskIntoConstraints = false
heightAnchor.constraint(equalToConstant: height).isActive = true
widthAnchor.constraint(equalToConstant: width).isActive = true
}

func setHeight(_ height: CGFloat) {
translatesAutoresizingMaskIntoConstraints = false
heightAnchor.constraint(equalToConstant: height).isActive = true
}
}

class ImageSliderCell: UICollectionViewCell {
var imgView = UIImageView()


override init(frame: CGRect) {
super.init(frame: frame)

self.addSubview(imgView)
// not sure about the right size of image ...
imgView.setDimensions(height: 100.0, width: 100.0)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

关于ios - subview 未添加某些 UICollectionViewCells 和闪烁(以编程方式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66153530/

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