Reputation: 358
I have a collectionView and one textView inside its cell. When to textView keyboard appears but then collectionView moves up then return to previous position. How to stop it? How to stop moving collectionView when keyboard appears?
Here is my plain and very simple code:
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
}
This is example of uicollectionView moving after keyboard appeared.
Upvotes: 0
Views: 478
Reputation: 6110
This happens to prevent the keyboard from hiding textFields. If you want to stop that behavior (I don't recommend that) override viewWillAppear(_:)
without calling super.viewWillAppear(animated)
. Like this:
override func viewWillAppear(_ animated: Bool) {
}
Please note that overriding viewWillAppear(_:)
without calling the super method may cause undefined behavior.
Upvotes: 1