Search code examples
swiftkeyboarduitextfielduikeyboardnsnotifications

TextFiled above the Keyboard issue


I am working on App which has comments and users and I need the user to insert the comment into the table view , the issue that I am facing is about the keyboard where its when the user press the text field to write the comment the keyboard appears and the text field goes above it as the code below.

But the problem is when i change the language of the keyboard , change the keyboard to the Emoji or open the autocorrection the text field covered and won't move with the keyboard layout.

override func viewWillAppear(animated: Bool) {

    super.viewWillAppear(animated)

    // KeyBoard Show and Hide

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(Commants_Page.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object:nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(Commants_Page.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)


    NSNotificationCenter.defaultCenter().addObserver(self,selector: #selector(Commants_Page.adjustForKeyboard(_:)),name: UIKeyboardWillChangeFrameNotification,object: nil)

}


// KeyBoard Show and Hide Function

func keyboardWillShow(notification: NSNotification) {

    if KeyBoardMove == false {

        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            self.view.frame.origin.y -= keyboardSize.height

            KeyBoardMove = true
        }
    }
}

func keyboardWillHide(notification: NSNotification) {

    if KeyBoardMove == true {

        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
            self.view.frame.origin.y += keyboardSize.height

            KeyBoardMove = false
        }
    }
}

Screenshot


Solution

  • Once I faced the same issue and found a work around as below. I am not sure if this is the perfect solution but its working as expected for me. So you can give it a try.

    What I did is :

    1. Add UITextInputCurrentInputModeDidChangeNotification notification in viewWillAppear
    2. Implement changeInputMode method to determine keyboard type
    3. Handle keyboardWillShow and keyboardWillHide

      var isEmoji = Bool()
      
      override func viewWillAppear(animated: Bool) {
      
      super.viewWillAppear(animated)
      
      isEmoji = false
       NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeInputMode:", name:UITextInputCurrentInputModeDidChangeNotification, object: nil)
      
      }
      
      func changeInputMode(notification : NSNotification)
      {
          //Emoji keyboard does not have any primary language whereas normal text keyboard does have. Therefore, on the bases of it we can determine the keyboard type
      
          if let lang = txtComment.textInputMode?.primaryLanguage
          {
              isEmoji = false
          }
          else{
              isEmoji = true
          }
      }
      
      func keyboardWillShow(notification: NSNotification) {
          if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
              print(isEmoji)
              if(!isEmoji){                
      
                  writeCommentView.frame.origin.y -= keyboardSize.height
              }
              else{
                  writeCommentView.frame.origin.y -= 37 //where 37 is the height differece between normal keyboard and emoji keyboard. Change this value accordingly
              }
      
      
          }
      }
      
      func keyboardWillHide(notification: NSNotification) {
          if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
              //Handle this method accordingly. For example..
              if(!isEmoji){                
      
                  writeCommentView.frame.origin.y += keyboardSize.height
              }
              else{
                  writeCommentView.frame.origin.y += 37
              }
          }
      }
      

      Hope it will help.