显示键盘时向上或向下移动视图

Moving a View up or Down when a Keyboard is Shown

   NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow(_:)), name:NSNotification.Name.UIKeyboardWillShow, object: self.view.window)
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide(_:)), name:NSNotification.Name.UIKeyboardWillHide, object: self.view.window)

}
override func viewWillDisappear(_ animated: Bool) {
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: self.view.window)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: self.view.window)
}

    func keyboardWillHide(_ sender: Notification) {
    if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
    if self.view.frame.origin.y != 0{
    self.view.frame.origin.y += keyboardSize.height
}


    func keyboardWillShow(_ sender: NSNotification) {
        if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey]! as? NSValue)?.cgRectValue {
        if let offset = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey]! as? NSValue)?.cgRectValue {
            if self.view.frame.origin.y == 0{
                self.view.frame.origin.y -= keyboardSize.height

        if keyboardSize.height == offset.height {
        if self.view.frame.origin.y == 0 {
            UIView.animate(withDuration: 0.15, animations: {
                self.view.frame.origin.y -= keyboardSize.height
})
        }
    }
    else {
        UIView.animate(withDuration: 0.15, animations: {
            self.view.frame.origin.y += keyboardSize.height - offset.height
            })
    }
}

没用。它一直显示错误“type 'ViewController' has no member 'keyboardWillShow' Did you mean 'keyboardWillHideenter image description here

您的代码有几个问题。首先,方法 keyboardWillShow 和 keyboardWillHide 应该暴露给 Objective-c,其次,你的 open/close 花括号搞砸了。

我对上面的代码做了一些修改,试试看:

@objc func keyboardWillHide(_ sender: Notification) {
    if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y != 0 {
            self.view.frame.origin.y += keyboardSize.height
        }
    }
}

@objc func keyboardWillShow(_ sender: NSNotification) {
    if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey]! as? NSValue)?.cgRectValue,
        let offset = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey]! as? NSValue)?.cgRectValue {

        if self.view.frame.origin.y == 0 {

            self.view.frame.origin.y -= keyboardSize.height

            if keyboardSize.height == offset.height {
                if self.view.frame.origin.y == 0 {
                    UIView.animate(withDuration: 0.15, animations: {
                        self.view.frame.origin.y -= keyboardSize.height
                    })
                }
            }
            else {
                UIView.animate(withDuration: 0.15, animations: {
                    self.view.frame.origin.y += keyboardSize.height - offset.height
                })
            }
        }

    }
}

您可能还想放弃这个 altogeter 并使用 IHKeyboardAvoiding 组件来解决键盘隐藏您的字段的问题。