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:
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
Hallo, haben Sie die Lösung für dieses Problem gefunden, jetzt bin ich mit diesem Problem konfrontiert. Vorschläge sind willkommen – karthik