我在其单元格内有一个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
}
答案 0 :(得分:1)
这是为了防止键盘隐藏文本字段。如果您想停止该行为(我不建议),则在不调用viewWillAppear(_:)
的情况下覆盖super.viewWillAppear(animated)
。像这样:
override func viewWillAppear(_ animated: Bool) {
}
请注意,不调用super方法而覆盖viewWillAppear(_:)
可能会导致未定义的行为。