修复 - 键盘移动整个集合视图,留下空白区域

时间:2018-03-10 23:37:55

标签: ios swift layout uicollectionview

在我的Collection ViewController中,我有Collection View CelltextField。每当我按下textField并且键盘显示时,view就会向上移动,留下空格......

显示键盘之前 everything is ok

显示键盘后 cell layout is up...,您无法看到整个单元格内容。

我在下面尝试了一个答案解决方案,我得到了这个:black view on top

1 个答案:

答案 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
    }
}