我有一个水平的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) { }
- }
- }
CollectionViewController:
- 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
- }
- }
非常感谢
解决方法
首先,我必须给我的两分钱,故事板很棒:)
对于此用例,您可能不想使用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,for: indexPath)
- // Configure the cell
- return cell
- }
- }
最后,您可以删除CollectionViewController,因为它已被替换.
PS你也可能想要1)扩展ViewController以符合UICollectionViewDelegateFlowLayout和2)使collectionView全局.