Moving a View up or Down when a Keyboard is Shown

42 Views Asked by At
   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 'keyboardWillHideenter image description here

1

There are 1 best solutions below

0
sciasxp On BEST ANSWER

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.