【问题标题】:CollectionView moves up when keyboard appears then returns to previous position当键盘出现时 CollectionView 向上移动然后返回到之前的位置
【发布时间】:2018-10-15 20:09:07
【问题描述】:

我在其单元格中有一个 collectionView 和一个 textView。何时出现 textView 键盘但随后 collectionView 向上移动然后返回到先前位置。如何阻止它?出现键盘时如何停止移动collectionView?

这是我简单明了的代码:

 class MainReadAssistantViewController: UICollectionViewController, 
UICollectionViewDelegateFlowLayout {
let wordOrPhraseCellId = "wordOrPhraseCellId"

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
   collectionView?.backgroundColor = UIColor.blue
collectionView?.register(WordOrPhraseCell.self, forCellWithReuseIdentifier: wordOrPhraseCellId)

    collectionView?.register(SentenceOrASuperLongPhrase.self, forCellWithReuseIdentifier: sentenceOrASuperLongPhraseCellId)
    collectionView?.isScrollEnabled = false

   // collectionView?.alwaysBounceVertical = true
     }

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


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {


    return CGSize(width: view.frame.width, height: view.frame.height - 150)
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    var cell = collectionView.dequeueReusableCell(withReuseIdentifier: 
        return cell

        }
    }


    return cell
}

这是键盘出现后 uicollectionView 移动的示例。

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewlayout uikeyboard


    【解决方案1】:

    这是为了防止键盘隐藏文本字段。如果您想停止这种行为(我不建议这样做)覆盖 viewWillAppear(_:) 而不调用 super.viewWillAppear(animated)。像这样:

    override func viewWillAppear(_ animated: Bool) {
    
    }
    

    请注意,在不调用 super 方法的情况下覆盖 viewWillAppear(_:) 可能会导致未定义的行为。

    【讨论】:

      猜你喜欢
      • 2015-04-18
      • 1970-01-01
      • 2011-12-18
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      相关资源
      最近更新 更多