2017-08-17 67 views
1

我使用UIColletion视图与自定义单元格与里面的UITextField。正如我发现的那样,当textField成为第一响应者时,collectionView会自动滚动,所以键盘不会覆盖编辑的字段。UICollectionView becomeFirstResponder错误的行为

问题是,此功能无法正常工作。它将文本字段滚动到键盘正上方的位置,但接下来滚动一点点,键盘隐藏文本字段的一部分。

我尝试了只有一个简单的收集视图,只有一个原型单元格与其中的文本字段的新项目,它也不工作。

在我的原始项目中有更复杂的单元格,第二个滚动要大得多,所以整个文本字段都在键盘下。

这是集合视图中的错误还是我做错了什么? (在这个简单的项目有几乎不可能什么错....

+0

你能添加一些你正在使用的代码在新的,小的项目? –

+0

发表于新的回答 –

+0

@MartinKubišta请检查答案下面。添加了一个片段,看起来像一个更灵活的解决方案 –

回答

0

至于我的样本项目。

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

0

只是重新创建了你的项目,似乎在工作对于我在iPhone 7 plus上进行测试很不错。

集合视图滚动并显示文本字段。现在有一个问题是键盘下面的填充不总是相同的。我的建议是确保您的文本框已经到位,然后重试。这可能会有所作为。

另一方面,我会建议使用ScrollView而不是集合视图。 集合视图具有单元重用,这可能会导致重新使用单元格和文本字段被释放的问题。

具有很多TextField的形式时,我最常做的是:

  • 创建滚动视图和引脚到所有的边缘。尤其是底部约束很重要
  • 在scrollView中添加视图以将其固定到所有边,并使您包含的UIView的宽度和高度等于ScrollViews超级视图的宽度和高度。 (这样滚动插图将正确比例)
  • 作为子视图添加您的文本框的UIView的上述
  • 添加键盘通知观察员在UIViewController和滚动型的底部约束动画键盘的高度和回0当键盘从屏幕移动时。

通过这种方式,您可以确保控制屏幕动画,并且如果您觉得有必要,可以添加更多的填充。 scrollView将处理抵抗并让你的textField放置在正确的视口中。

此外,您将能够引用所有的textField。通过创建插座或将它们添加到OutletCollection中。

我通常会做后者为了保持他们的秩序,并将焦点移到列表中的下一个。

import UIKit 

class KeyboardViewController: UIViewController { 

    @IBOutlet weak var bottomScrollViewConstraint: NSLayoutConstraint! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
     NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
    } 

    @IBAction func hideKeyboard(_ sender: Any) { 
     self.view.endEditing(true) 
    } 

    func keyboardWillShow(notification: NSNotification) { 

     if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { 
      self.bottomScrollViewConstraint.constant = keyboardSize.height //Add more padding here if you want 
      UIView.animate(withDuration: 0.8, animations: { 
       self.view.layoutIfNeeded() 
      }) 
     } 

    } 

    func keyboardWillHide(notification: NSNotification) { 
     self.bottomScrollViewConstraint.constant = 0 
     UIView.animate(withDuration: 0.8, animations: { 
      self.view.layoutIfNeeded() 
     }) 
    } 
} 

enter image description here

+0

我知道这个解决方案,但它不能在我们的项目中完成。整个屏幕是某种形式的,具有许多不同类型的输入和文本字段只是这种输入中的一种。它不是静态的,而是完全由我们的collectionview子类中的代码生成,它处理一些布局变化,基本输入,项目重新排序等等。 –

+0

仍然集合视图的自主行为看起来像一个bug。我用辅助视图试了一下,它的表现也一样。 https://youtu.be/lN-I59qF2cY –

相关问题