CollectionViewCell滚动到错误位置

问题描述 投票:0回答:1

我有一个collectionView固定在视图的上(如第1张照片所示),它具有isPagingEnabled = true。我还隐藏了navigationBar。

[cv第一次加载时,单元格错误地显示在设备顶部的下方(如第二张照片所示。]

当我滚动/翻页时,单元格正确地显示在凹口后面(第三张照片)。

如果我滚动/翻页回到第一个单元格,它将正确定位自己(第三张照片)。但是,如果我再拉下简历,则单元会返回到错误的位置(第二张照片)。

我还注意到,当单元处于错误的位置时,如果我点击它,它会自动滚动到正确的位置。

我尝试过这个answer和这个answer,但都没有用。我在函数中调用了view.layoutIfNeeded()collectionView.layoutIfNeeded()

<<

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.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 // https://stackoverflow.com/a/56639186/4833705 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 } 第一张照片,简历锚,顶部固定在槽口后面视图的顶部。

enter image description here

第二张照片,第一次在槽口后显示不正确,当在第一个照片上时,将cv下拉时显示不正确。

enter image description here

第三张照片,在滚动/分页时,单元格正确显示在缺口的后面

enter image description here

我有一个collectionView固定在视图的上(如第一张照片所示),并且isPagingEnabled = true。我还隐藏了navigationBar。当cv第一次加载时,单元格...

ios swift uicollectionview uicollectionviewcell uicollectionviewlayout
1个回答
0
投票
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) navigationController?.setNavigationBarHidden(true, animated: false) self.view.layoutIfNeeded() }
© www.soinside.com 2019 - 2024. All rights reserved.