【发布时间】: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