2016-07-09 9 views
1

Ich arbeite an App, die Kommentare und Benutzer hat und ich brauche den Benutzer, um den Kommentar in die Tabellenansicht einfügen, das Problem, das ich gegenüber der Tastatur ist, wenn der Benutzer den Text drücken Feld, um den Kommentar zu schreiben, erscheint die Tastatur und das Textfeld geht darüber wie der Code unten.TextFiled über der Tastatur Ausgabe

Aber das Problem ist, wenn ich die Sprache der Tastatur ändern, ändern Sie die Tastatur auf die Emoji oder öffnen Sie die Autokorrektur das Textfeld abgedeckt und wird nicht mit dem Tastaturlayout bewegen.

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

Antwort

0

Einmal stand ich das gleiche Problem und fand um wie unten eine Arbeit. Ich bin mir nicht sicher, ob dies die perfekte Lösung ist, aber es funktioniert wie erwartet für mich. So können Sie es versuchen.

Was ich getan habe ist:

  1. hinzufügen UITextInputCurrentInputModeDidChangeNotification Benachrichtigung in viewWillAppear
  2. changeInputMode Methode implementieren Tastaturtyp
  3. Griff keyboardWillShow und 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 
         } 
        } 
    } 
    

    Hoffnung zu bestimmen, es hilft.

+0

Dank seiner mir geholfen, aber in deferent Art und Weise, aber immer noch ist es nicht die perfekte Art und Weise, weil, wenn der Benutzer die prädiktive Option öffnen die eingereichten Text verschwinden –

+0

Dann gibt es viele Bibliotheken zur Verfügung, die es verarbeiten kann. Sie können einen Blick auf https://github.com/michaeltyson/TPKeyboardAvoiding gehen – iRiziya

+0

Dank iRiziya werde ich es überprüfen –

Verwandte Themen