4

I am new to iOS development.
I am working with view with multiple TextField and TextView.I am trying to move the view up to avoid keyboard is hiding the Editing TextField,TextView content.The below code is working fine for all TextFields but it is not moving view up when TextView is Edited.Hope you understand my problem. Thanks in advance.

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: .UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: .UIKeyboardWillHide, object: nil)
}

deinit {
    NotificationCenter.default.removeObserver(self)
}  

 var keybordenabled = false
@objc func keyboardWillShow(notification: NSNotification) {
      if(keybordenabled == false){
    adjustInsetForKeyboardShow(show: true, notification: notification)
          keybordenabled = true
    } 
}

@objc func keyboardWillHide(notification: NSNotification) {
     keybordenabled = false

    adjustInsetForKeyboardShow(show: false, notification: notification)
}

func adjustInsetForKeyboardShow(show: Bool, notification: NSNotification) {
    let userInfo = notification.userInfo ?? [:]
    let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
    let adjustment = (keyboardFrame.height * (show ? 1 : -1)) + 20

    scrollView.contentInset.bottom += adjustment
    scrollView.scrollIndicatorInsets.bottom += adjustment
    self.view.layoutIfNeeded()
}



func textViewDidBeginEditing(_ textView: UITextView)
{
    if (textView.text == "Enter comment here...")
    {
        textView.text = ""
        textView.textColor = .black
    }
    textView.becomeFirstResponder() 
}

func textViewDidEndEditing(_ textView: UITextView)
{
    if (textView.text == "")
    {
        textView.text = "Enter comment here..."
        textView.textColor = .lightGray
    }
    textView.resignFirstResponder()
}
John
  • 3,769
  • 4
  • 12
  • 23

2 Answers2

2
This is  work Me!

override func viewWillAppear(_ animated: Bool) {
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear), name: Notification.Name.UIKeyboardWillHide, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear), name: Notification.Name.UIKeyboardWillShow, object: nil)
    }


override func viewWillDisappear(_ animated: Bool) {
        NotificationCenter.default.removeObserver(self)
    }


 @objc func keyboardWillAppear(_ 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 keyboardWillDisappear(_ 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
            }
        }
    }
Keyur Faldu
  • 195
  • 9
  • 2
    You should be using `UIKeyboardFrameEndUserInfoKey` instead of `UIKeyboardFrameBeginUserInfoKey`. The naming pretty much explains itself but for further reference you can check it here : https://stackoverflow.com/questions/3332364/uikeyboardframebeginuserinfokey-uikeyboardframeenduserinfokey – CoderPug Sep 26 '18 at 04:44
  • in keyboardWillDisappear you can also set the self.view.frame.origin.y = 0 its also working. – Anil Kumar Jun 25 '19 at 12:01
0

@Jhon You can use IQKeyboard third party library for handling textfields and textviews scrolling appearance of keyboard.  Using this IQKeyboard SDK will manage auto scrolling of all your textfield and textView, Also it will save your lot's of work and time.

Please refer this link: https://github.com/hackiftekhar/IQKeyboardManager

  • Thanks......I have some complex views(Text view inside Embedded Container view,....) .....IQKeyboard manager is not working as expected.... – John Feb 09 '18 at 10:33