0

I have general message view controller I am sending message using textfield but keyboard is covered the text fields. I can refer Move view with keyboard using Swift this link added some code but after that some portion shows black. how to avoid the screen black after sending the message

enter image description here

enter image description here

enter image description here

this is code

   override func viewDidLoad() {
            super.viewDidLoad()
            NotificationCenter.default.addObserver(self, selector: #selector(GenralMessageViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
            NotificationCenter.default.addObserver(self, selector: #selector(GenralMessageViewController.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 -= keyboardSize.height
           }
        }
    }

   @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 += keyboardSize.height
           }
        }
   }
Kamran
  • 14,987
  • 4
  • 33
  • 51
naga
  • 397
  • 2
  • 12
  • 26

2 Answers2

1

Your code is just wrong. You have no business changing the frame of self.view. The main view of a view controller must be left in place where it is. Wrap your interface in a subview of the main view and move the subview (if you insist on using this approach to avoiding being covered by the keyboard).

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I second this answer. use a constraint, a scroll view, a sub view, a container... out of all the ways you can do it, moving your main view isn't one. – Aju Antony Apr 19 '18 at 17:38
0

Try changing your end y origin back to 0 instead of a calculated value.

   @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 == 0
           }
        }
   }
Kamran
  • 14,987
  • 4
  • 33
  • 51
Jared
  • 793
  • 6
  • 16