在我的Collection ViewController
中,我有Collection View Cell
和textField
。每当我按下textField
并且键盘显示时,view
就会向上移动,留下空格......
显示键盘之前 everything is ok
显示键盘后 cell layout is up...,您无法看到整个单元格内容。
我在下面尝试了一个答案解决方案,我得到了这个:black view on top
答案 0 :(得分:0)
您可以为键盘显示和隐藏时添加观察者并相应地移动视图:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
let newYOrigin = CGFloat(100) // or whatever new value you want
self.yourView.frame.origin.y = newYOrigin
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
let newYOrigin = CGFloat(0) // or whatever new value you want
self.yourView.frame.origin.y = newYOrigin
}
}