2016-05-22 12 views
1

So erkennen Sie Tastaturhöhenänderungen oder Tastaturänderungen in iOS mit Swift.Tastaturhöhenveränderungsbeobachter

Ich bin in der Lage, einen Beobachter für meine App hinzufügen, ob die Tastatur zu erkennen ist Show oder nicht verwenden:

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

und ich meine Tastenposition Wechsel nach, dass:

func keyboardWillShow(notification: NSNotification) { 
     animateTextFieldWithKeyboard(notification) 
} 

func keyboardWillHide(notification: NSNotification) { 
     animateTextFieldWithKeyboard(notification) 
} 

func animateTextFieldWithKeyboard(notification: NSNotification) { 


let userInfo = notification.userInfo! 

let keyboardSize = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue() 
let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! Double 
let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as! UInt 

// baseContraint is your Auto Layout constraint that pins the 
// text view to the bottom of the superview. 

if notification.name == UIKeyboardWillShowNotification { 
    if (BottomConstraint.constant == 0) { 
     BottomConstraint.constant += keyboardSize.height 
    } 

    // move up 
} 
else { 
    BottomConstraint.constant = 0 
    // move down 
} 

view.setNeedsUpdateConstraints() 

let options = UIViewAnimationOptions(rawValue: curve << 16) 
UIView.animateWithDuration(duration, delay: 0, options: options, 
    animations: { 
     self.view.layoutIfNeeded() 
    }, 
    completion: nil 
) 

} 

Alles funktioniert gut, wie Sie im Screenshot sehen:

enter image description here

Aber das Problem kommt, wenn ich den Tastaturtyp zum Beispiel zu Emoji ändere. es versteckt sich meine TextField- und mein Knopf, so möchte ich die Position der Taste ändern und die TextFiend nach der Tastatur neue Höhe

enter image description here

+0

Hallo, haben Sie die Lösung für dieses Problem gefunden, jetzt bin ich mit diesem Problem konfrontiert. Vorschläge sind willkommen – karthik

Antwort

1

Wenn Sie auf UIKeyboardWillShowNotification in der Xcode-Dokumentation suchen Sie bekommen der Abschnitt auf UIWindow, der am Ende eine Tabelle mit Benachrichtigungen enthält.

Ich schlage vor, versuchen Sie die UIKeyboardWillChangeFrameNotification.

Zeit, um die Antwort zu finden: Etwa 30 Sekunden.

+0

Vielen Dank für Ihre Antwort, ich werde es versuchen und ich werde zu Ihnen zurückkommen. –

+0

Ich habe diese Funktion versucht und es gab mir die Höhe der Tastatur vor dem Wechsel. =. = Irgendwelche Updates? –

+0

Funktioniert nicht. Wenn die Tastatureingabe von Englisch oder einer anderen Sprache zu Emoji wechselt, wird die Benachrichtigung nicht ausgelöst. Zeit, um eine richtige Antwort zu finden? Ich werde Ihnen Bescheid geben. – Fmessina

0

Ich benutze dies und alle Benachrichtigungen werden ausgelöst.

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

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil) 

}  

@objc func keyboardWillShow(notification: NSNotification) { 
    let keyboardSize = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue 

     if keyboard == false{ 
      keyboard = true 
      lastKeyboardHeight = keyboardSize.height 
      chatDetailView.frame.origin.y = chatDetailView.frame.origin.y-(keyboardSize.height-bottomMenu.frame.height) 
     } 
    } 

    @objc func keyboardWillChange(notification: NSNotification) { 
     let keyboardSize1 = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue 

     if keyboard == true && lastKeyboardHeight != keyboardSize1.height { 
      if lastKeyboardHeight < keyboardSize1.height{ 
       let keyboardDifference: CGFloat = keyboardSize1.height-lastKeyboardHeight 
       chatDetailView.frame.origin.y -= keyboardDifference 

      } else { 
       let keyboardDifference: CGFloat = lastKeyboardHeight-keyboardSize1.height 
       chatDetailView.frame.origin.y += keyboardDifference 
      } 
      lastKeyboardHeight = keyboardSize1.height 
     } 
    } 

    @objc func keyboardWillHide(notification: NSNotification) { 
     if keyboard == true { 
      keyboard = false 
      chatDetailView.frame.origin.y = chatDetailView.frame.origin.y+(lastKeyboardHeight-bottomMenu.frame.height) 
     } 
    }