CollectionView 单元格在屏幕重新加载时更改大小

CollectionView Cell Changing Size when screen reloads

我正在创建一个屏幕,用户可以在其中搜索电影并将结果加载到集合视图中,一切都完美加载但是在模拟器中当我单击“命令,shift,A”以更改为灯光模式时确保颜色会正确适应单元格随机更改为全屏大小而不是我设置的大小。

当我离开应用程序进入主页然后单击返回应用程序时,也会发生这种情况。我正在以编程方式创建所有内容,因此需要以这种方式回答。

下面是我的自定义单元格中的代码:

import UIKit

class FavouritesCell: UICollectionViewCell {
    static let identifier = "FavouritesCell"

    let movieTitle: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.lineBreakMode = .byWordWrapping
        label.textColor = .secondaryLabel
        //label.text = "Title"
        label.adjustsFontSizeToFitWidth = true
        label.textAlignment = .center
        label.font = label.font.withSize(12)
        label.minimumScaleFactor = 0.75
        return label 
    }()

    let image : UIImageView = {
        let image = UIImageView()
        image.clipsToBounds = true
        image.translatesAutoresizingMaskIntoConstraints = false
        //image.backgroundColor = .yellow
        image.layer.cornerRadius = 10.0
        return image
    }()

    let cancelItem : UILabel = {
        let label = UILabel()
        label.layer.borderWidth = 2.0
        label.layer.borderColor = UIColor.systemGray.cgColor
        label.text = "X"
        label.textAlignment = .center
        label.layer.cornerRadius = 15
        //label.isHidden = true
        return label
    }()

    override init(frame: CGRect) {
        super.init(frame: .zero)
    
        contentView.addSubview(image)
        contentView.addSubview(movieTitle)
        contentView.addSubview(cancelItem)
    }

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

    override func layoutSubviews() {
        super.layoutSubviews()
        image.frame = CGRect(x: 0, y: 25, width: contentView.width, height: contentView.height - 30 )
        movieTitle.frame = CGRect(x: 0, y: 0, width: contentView.width, height: 25)
        cancelItem.frame = CGRect(x: image.right - 25, y: image.top , width: 25, height: 25)
    }
}

下面是与 collectionView 相关的视图控制器的代码:

private let collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical
    layout.minimumLineSpacing = 2
    layout.minimumInteritemSpacing = 2
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
    collectionView.translatesAutoresizingMaskIntoConstraints = false
    collectionView.register(FavouritesCell.self, forCellWithReuseIdentifier: FavouritesCell.identifier)
    collectionView.clipsToBounds = true
    collectionView.backgroundColor = UIColor.systemBackground
    
    return collectionView
}()
    NSLayoutConstraint.activate([
        searchText.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
        searchText.heightAnchor.constraint(equalToConstant: 50),
        searchText.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        searchText.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        
        collectionView.topAnchor.constraint(equalTo: searchText.bottomAnchor),
        collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
    ])
}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: (view.width / 3) - 4, height: (view.width / 2) - 2 )
}

image of CollectionView working normally

After the screen has been reloaded

尝试给你的 imageView 一个固定的高度和固定的宽度,然后再试一次,它会起作用