0

I have a textview and a text field on my screen and I want move screen when edit textview. This is my code

    override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: .UIKeyboardWillShow, object: nil)

 //   NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: .UIKeyboardWillHide, object: nil)

    print("addd observer")

}
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil)
 //   NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide, object: nil)
}

@objc func keyboardWillShow(_ sender: Notification) {
    let keyboardSize = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
    print("keyboardwillshow")
    ksize = keyboardSize?.height

}


@objc func textViewDidBeginEditing(_ textView: UITextView) {
        print("textviewedit")
        boxview.frame.origin.y -= ksize
        print("textview")



}
@objc func textViewDidEndEditing(_ textView: UITextView) {

    print("textvieweditend")
    boxview.frame.origin.y = 0

}

It works when I just edit textview. However, if I edit the textview right after focus on the textfield(without exit keyboard) it doens't work. I already checked that textviewDidBeginEditing() is called but the screen doesn't move.. How can I solve this? All advises are welcome!

  • Instead of using `boxview.frame.origin.y` to change frame, I suggest to use `boxview.center`. Maybe it will work. – trungduc Jun 24 '18 at 08:30
  • @trungduc thanks for your advice. I tried but it seems in same situation. It works when I just edit textview but, when I edit textview, after editing text field, it doesn't work.... – jun Hong Park Jun 24 '18 at 11:05
  • Please refer to this answer : https://stackoverflow.com/a/31124676/3882414 – rami Jun 24 '18 at 11:05
  • Please refer to these answers: https://stackoverflow.com/a/31124676/3882414 https://stackoverflow.com/a/46140997/3882414 – rami Jun 24 '18 at 11:07
  • @rami Thanks rami!!!!! I add self.view.Layoutifneeded() to my keyboardwillappear() and it works! I catch clue by your advice!! Repeat, Thanks u very much!!! – jun Hong Park Jun 24 '18 at 11:40

1 Answers1

0

I add self.view.Layoutifneeded() to keyboardwillchange() and it works!! thanks everybody.