屏幕重新加载时 CollectionView 单元格更改大小
Posted
技术标签:
【中文标题】屏幕重新加载时 CollectionView 单元格更改大小【英文标题】:CollectionView Cell Changing Size when screen reloads 【发布时间】:2020-12-05 16:36:06 【问题描述】:我正在创建一个屏幕,用户可以在其中搜索电影并将结果加载到集合视图中,一切都完美加载,但在模拟器中,当我单击“命令、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
【问题讨论】:
是否有图片显示问题? 我添加了前后截图来帮助 【参考方案1】:尝试给你的 imageView 一个固定的高度和固定的宽度,然后再试一次,它会起作用
【讨论】:
以上是关于屏幕重新加载时 CollectionView 单元格更改大小的主要内容,如果未能解决你的问题,请参考以下文章
如何在不重新加载 visibleCell 的情况下在 collectionView 上重新加载数据
如何在不重新加载图像的情况下重新加载 collectionview 单元格