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

时间:2018-07-08 12:53:11

标签: xcode xcode6 xcode4

   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
            })
    }
}

它不起作用。它继续显示错误“类型'ViewController'没有成员'keyboardWillShow'您是说'keyboardWillHide enter image description here

1 个答案:

答案 0 :(得分:0)

您的代码有两个问题。首先,应该将KeyboardWillShow和keyboardWillHide方法暴露给Objective-c,其次,打开/关闭卷曲表钉弄乱了。

我在上面的代码中做了一些修改,像这样尝试:

@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 组件来解决键盘隐藏字段的问题。

相关问题