【问题标题】:UICollectionView jumps when beginning the scroll when using large title使用大标题时开始滚动时 UICollectionView 跳转
【发布时间】:2020-05-06 23:58:25
【问题描述】:

在我自己的一个应用中看到这一点后,我构建了一个测试应用。 我在导航控制器中嵌入了一个集合视图。

这都与 ViewController 类相关联。

class ViewController: UIViewController {

    private let reuseIdentifier = "userCell"
    @IBOutlet weak var collectionView: UICollectionView!

    let users = Stub.users

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.automaticallyAdjustsScrollIndicatorInsets = false
        navigationController?.navigationBar.prefersLargeTitles = true
    }
}

// MARK: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout

extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.users.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! UserCollectionViewCell
        cell.setup(user: self.users[indexpath.row])
        return cell
    }
}

// MARK: UICollectionViewDelegateFlowLayout

extension ViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let hight = view.frame.height
        let width = view.frame.width

        return CGSize(width: width * 0.3, height: hight * 0.185)
    }
}

所有功能都可以正常工作,但是当我滚动时,集合视图的顶部会跳转。 这只发生在我将prefersLargeTitles 设置为true 时。或者,如果我在导航栏中有一个搜索栏。

你如何解决这个问题,这样它就不会跳来跳去? 如果您有任何问题,请提出。 谢谢。

【问题讨论】:

  • 更改后会发生什么:collectionView.automaticallyAdjustsScrollIndicatorInsets = false
  • 同样的行为仍然存在。

标签: ios swift uicollectionview


【解决方案1】:

我能够通过将collectionView 限制在超级视图的顶部而不是safeArea 顶部来解决这个问题。

可能并非在所有情况下都是可行的解决方案,因为您自己的用例可能需要将其固定到 safeArea,但这似乎是原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多