gpt4 book ai didi

ios - 键盘打破了 UICollectionViewController 中的布局

转载 作者:IT王子 更新时间:2023-10-29 05:52:21 26 4
gpt4 key购买 nike

我有一个水平的 UICollectionViewController,其中每个单元格的底部都包含一个 UITextView。当我在 UITextView 内部点击时,当键盘出现时,CollectionView 的高度减少了 260 点(我注意到这是键盘的高度)然后增加了 130 点,所以最终高度比期望的低 130 点。

你知道为什么框架会以这种方式改变吗?

我在下面包含了最相关的部分,或者您可以在这里找到测试项目:https://github.com/johntiror/testAutomaticPush/tree/master

UIViewController(简单地启动 CollectionViewController):

class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

let layout = UICollectionViewFlowLayout()
layout.itemSize = view.bounds.size
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
let fsPicVC = CollectionViewController(collectionViewLayout: layout)
self.present(fsPicVC, animated: true) { }
}
}

Collection View Controller :

class CollectionViewController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()

self.collectionView!.register(CollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)

return cell
}
}

enter image description here

非常感谢

最佳答案

首先,我必须给两分钱, Storyboard很棒 :)

对于此用例,您可能不想使用 CollectionViewController。如果您决定使用它,我还发布了另一个答案。这是将 CollectionView 移动到 ViewController 的最快方法。这解决了您的问题,但不考虑自动布局。

1) 在 ViewController 中替换这些行:

let fsPicVC = CollectionViewController(collectionViewLayout: layout)
self.present(fsPicVC, animated: true) { }

let collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
collectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.dataSource = self
view.addSubview(collectionView)

2) 将其添加到 ViewController 的最底部(在 ViewController 类之外):

extension ViewController: UICollectionViewDataSource {

func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)

// Configure the cell

return cell
}
}

最后,您可以删除 CollectionViewController,因为它现在已被替换。

PS 您可能还想 1) 扩展 ViewController 以符合 UICollectionViewDelegateFlowLayout 和 2) 使 collectionView 全局化。

关于ios - 键盘打破了 UICollectionViewController 中的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43704493/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com