0

I am trying to move my textfield up when keyboard use to show and again it goes down when keyboard hides. So there I am using NotificationCenter.default.addObserver but this is working fine only when it is charging and when I remove from charger it is not working i.e it does not move what may be the case. Please help me out with this thing. Thanks in advance

Below is my code :

 override func viewDidLoad() {
    super.viewDidLoad()

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

   @objc func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y != 0{
            self.view.frame.origin.y -= 50
        }
    }
}

@objc func keyboardWillHide(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y != 0{
            self.view.frame.origin.y += 50
        }
    }
}
Dimple
  • 788
  • 1
  • 11
  • 27
Ashish yadav
  • 117
  • 2
  • 11

2 Answers2

0

The problem is that you are using the UIKeyboardFrameBeginUserInfoKey instead of UIKeyboardFrameEndUserInfoKey.

The code should be:

@objc func keyboardWillShow(_ notification: Notification) {
    if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
        let keyboardRectangle = keyboardFrame.cgRectValue
        let keyboardHeight = keyboardRectangle.height
    }
}
  • Did you check the value you are getting for the keyboard height? – Binoj V Janardhanan Sep 18 '18 at 07:48
  • Yes I am getting keyboard value, when i am attaching charger it is working – Ashish yadav Sep 18 '18 at 07:52
  • Another suggestion would be to change the view that you are moving up and down. Use another container view inside the main view of the ViewController and adjust the position of the container view. If you have auto layout, move the views by changing the constraints values rather than changing the frames of the view. – Binoj V Janardhanan Sep 18 '18 at 07:54
  • Well, the charger part is weird but that doesn't give you anything. Have a look at the suggestion in the last comment and see if it makes any difference. The frame change to the main view could be the issue if you are getting the keyboard value correctly. – Binoj V Janardhanan Sep 18 '18 at 08:27
0

If self.view.frame.origin.y is 0, then nothing happen. Right ?

@objc func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        // If `origin.y` is `0`, then do nothing.
        if self.view.frame.origin.y != 0{
            self.view.frame.origin.y -= 50
        }
    }

@objc func keyboardWillHide(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        // If `origin.y` is `0` in `keyboardWillShow`, 
        // it will still be `0` here.
        // So, do nothing. 
        if self.view.frame.origin.y != 0{
            self.view.frame.origin.y += 50
        }
    }
}
AechoLiu
  • 17,522
  • 9
  • 100
  • 118