CollectionViewCell 滚动到错误的位置
CollectionViewCell scrolls to wrong position
我有一个固定到视图顶部的 collectionView(如第一张照片所示),它有 isPagingEnabled = true
。我也隐藏了导航栏。
第一次加载简历时,单元格错误地显示在设备的顶部缺口下方(如第 2 张照片所示)。
当我 scroll/page 时,单元格在凹口后面正确显示自己(第 3 张照片)。
如果我 scroll/page 返回到第一个单元格,它会正确定位自己(第 3 张照片)。但是,如果我随后将 cv 下拉,单元格会弹回到不正确的位置(第 2 张照片)。
我还注意到当单元格处于错误位置时,如果我点击它,它会自行滚动到正确位置。
我尝试了这个 answer and this answer 但都没有用。我在函数中调用了 view.layoutIfNeeded()
和 collectionView.layoutIfNeeded()
。
为什么当 cv 第一次加载时和我拉动刷新时单元格显示不正确,但当我向上或向下翻页时它显示正确?
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.delegate = self
collectionView.dataSource = self
collectionView.alwaysBounceVertical = true
collectionView.showsVerticalScrollIndicator = false
collectionView.backgroundColor = .white
collectionView.register(HomeCell.self, forCellWithReuseIdentifier: homeCell)
collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
collectionView.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
collectionView.isPagingEnabled = true
return collectionView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setCollectionViewAnchors()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: false)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.frame.width
let height = collectionView.frame.height
return CGSize(width: width, height: height)
}
func setCollectionViewAnchors() {
let homeIndicatorHeight: CGFloat = 34 //
view.addSubview(collectionView)
collectionView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
collectionView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -homeIndicatorHeight).isActive = true
}
第一张照片,简历锚点,顶部固定在缺口后面视图的最顶部。
第二张照片,单元格第一次显示不正确,在第一个单元格上时,下拉 cv 时显示不正确。
第 3 张照片,当 scrolling/paging
时,单元格在凹口后面正确显示
尝试在viewWillAppear()
方法中调用layoutIfNeeded()
,像这样:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: false)
self.view.layoutIfNeeded()
}
我找到了。
我必须将这一行添加到我的 collectionView 中:
collectionView.contentInsetAdjustmentBehavior = .never
我有一个固定到视图顶部的 collectionView(如第一张照片所示),它有 isPagingEnabled = true
。我也隐藏了导航栏。
第一次加载简历时,单元格错误地显示在设备的顶部缺口下方(如第 2 张照片所示)。
当我 scroll/page 时,单元格在凹口后面正确显示自己(第 3 张照片)。
如果我 scroll/page 返回到第一个单元格,它会正确定位自己(第 3 张照片)。但是,如果我随后将 cv 下拉,单元格会弹回到不正确的位置(第 2 张照片)。
我还注意到当单元格处于错误位置时,如果我点击它,它会自行滚动到正确位置。
我尝试了这个 answer and this answer 但都没有用。我在函数中调用了 view.layoutIfNeeded()
和 collectionView.layoutIfNeeded()
。
为什么当 cv 第一次加载时和我拉动刷新时单元格显示不正确,但当我向上或向下翻页时它显示正确?
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.delegate = self
collectionView.dataSource = self
collectionView.alwaysBounceVertical = true
collectionView.showsVerticalScrollIndicator = false
collectionView.backgroundColor = .white
collectionView.register(HomeCell.self, forCellWithReuseIdentifier: homeCell)
collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
collectionView.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
collectionView.isPagingEnabled = true
return collectionView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setCollectionViewAnchors()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: false)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.frame.width
let height = collectionView.frame.height
return CGSize(width: width, height: height)
}
func setCollectionViewAnchors() {
let homeIndicatorHeight: CGFloat = 34 //
view.addSubview(collectionView)
collectionView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
collectionView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -homeIndicatorHeight).isActive = true
}
第一张照片,简历锚点,顶部固定在缺口后面视图的最顶部。
第二张照片,单元格第一次显示不正确,在第一个单元格上时,下拉 cv 时显示不正确。
第 3 张照片,当 scrolling/paging
时,单元格在凹口后面正确显示尝试在viewWillAppear()
方法中调用layoutIfNeeded()
,像这样:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: false)
self.view.layoutIfNeeded()
}
我找到了
我必须将这一行添加到我的 collectionView 中:
collectionView.contentInsetAdjustmentBehavior = .never