ios - 使用大标题开始滚动时 UICollectionView 跳转

标签 ios swift uicollectionview

在我自己的一个应用程序中看到这一点后,我构建了一个测试应用程序。
我在导航 Controller 中嵌入了一个集合 View 。

main.storyboard

这一切都链接到 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)
    }
}


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

enter image description here

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

最佳答案

我能够通过约束 collectionView 来解决这个问题。到 super View 的顶部而不是 safeArea最佳。
可能不是在所有情况下都是可行的解决方案,因为您自己的用例可能需要将其固定到 safeArea但这似乎是原因。

关于ios - 使用大标题开始滚动时 UICollectionView 跳转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59832372/

相关文章:

iphone - 在 iOS 上使用 CoreData 处理类似于多个文档的东西

ios - 在 iOS 8 自定义键盘中显示建议栏

iphone - 如何使用 indexpath.row 从 UICollectionView 中删除项目

ios - UICollectionView水平分页布局

iphone - 调用 setCollectionViewLayout :animated does not reload UICollectionView

ios - 使用代码模拟 UIButton Click 事件

ios - 如何在 iOS (Swift) 上创建后台进程?

Swift:调用时出现 Segue 错误

ios - 火力基地和 swift

iphone - 在长按手势识别器上获取错误的 UIButton 标签