在我自己的一个应用程序中看到这一点后,我构建了一个测试应用程序。
我在导航 Controller 中嵌入了一个集合 View 。
这一切都链接到 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
.或者,如果我在导航栏中有一个搜索栏。你如何解决这个问题,这样它就不会跳来跳去?
如果您有任何问题,请提问。
谢谢你。
最佳答案
我能够通过约束 collectionView
来解决这个问题。到 super View 的顶部而不是 safeArea
最佳。
可能不是在所有情况下都是可行的解决方案,因为您自己的用例可能需要将其固定到 safeArea
但这似乎是原因。
关于ios - 使用大标题开始滚动时 UICollectionView 跳转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59832372/