菜鸟教程小白 发表于 2022-12-11 18:30:49

ios - UICollectionView becomeFirstResponder 错误行为


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

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

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

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

<p>这是 Collection View 中的错误还是我做错了什么? (在简单的项目中几乎不会有任何问题......</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>至于我的示例项目。</p>

<p>UICollectionViewController</p>

<pre><code>import UIKit

private let reuseIdentifier = &#34;Cell&#34;

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 .
      // Pass the selected object to the new view controller.
    }
    */

    // MARK: UICollectionViewDataSource

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


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

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

      cell?.field.backgroundColor = .red

      return cell ?? UICollectionViewCell()
    }

}
</code></pre>

<p>UICollectionViewCell</p>

<pre><code>import UIKit

class CollectionViewCell: UICollectionViewCell {
    @IBOutlet var field: UITextField!
}
</code></pre>

<p> <a href="/image/96JrM.png" rel="noreferrer noopener nofollow"><img src="/image/96JrM.png" alt="enter image description here"/></a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UICollectionView becomeFirstResponder 错误行为,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45739644/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45739644/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UICollectionView becomeFirstResponder 错误行为