OGeek|极客世界-中国程序员成长平台

标题: ios - UICollectionView becomeFirstResponder 错误行为 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:30
标题: ios - UICollectionView becomeFirstResponder 错误行为

我正在使用带有自定义单元格的 UIColletion View ,其中包含 UITextField。我发现,当 textField 成为第一响应者时,collectionView 会自动滚动,因此键盘不会覆盖已编辑的字段。

问题是,这个功能不能正常工作。它将文本字段滚动到键盘上方的正确位置,但接下来它会向下滚动一点,因为键盘隐藏了部分文本字段。

我尝试了一个新项目,只有一个简单的 Collection View ,其中只有一个带有文本字段的原型(prototype)单元格,但它也不起作用。

在我的原始项目中,单元格更复杂,第二个滚动条更大,因此整个文本字段都在键盘下方。

这是 Collection View 中的错误还是我做错了什么? (在简单的项目中几乎不会有任何问题......



Best Answer-推荐答案


至于我的示例项目。

UICollectionViewController

import UIKit

private let reuseIdentifier = "Cell"

class CollectionCollectionViewController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    // MARK: UICollectionViewDataSource

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 50
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? CollectionViewCell

        cell?.field.backgroundColor = .red

        return cell ?? UICollectionViewCell()
    }

}

UICollectionViewCell

import UIKit

class CollectionViewCell: UICollectionViewCell {
    @IBOutlet var field: UITextField!
}

enter image description here

关于ios - UICollectionView becomeFirstResponder 错误行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45739644/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4