简体   繁体   中英

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

It doesnt work. it keeps on showing an error "type 'ViewController' has no member 'keyboardWillShow' Did you mean 'keyboardWillHide enter image description here

There is a couple of problems with your code. First the methods keyboardWillShow and keyboardWillHide should be exposed to Objective-c, second, your open/close curly brakets are messed up.

I made some modifications in the above code, try it like this:

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

    }
}

You may also want to ditch this altogeter and use IHKeyboardAvoiding component to solve the issue of keyboard hiding your fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM