考虑到键盘的新QuickType部分.
是否真的只能使用UIKeyboardWillChangeFrameNotification的通知,
只是“不打扰”“老”UIKeyboardWillShowNotification和UIKeyboardWillHideNotification?
测试似乎显示它的工作完美,只使用keyboardFrameDidChange – 但我们可能会缺少一些东西?
这里是一个如何使用UIKeyboardWillChangeFrameNotification https://stackoverflow.com/a/26226732/294884的例子
解决方法
这绝对是可能的,可以把你的代码减少一半.以下示例使用自动布局进行大量重载.
NSNotificationCenter.defaultCenter().addObserverForName( UIKeyboardWillChangeFrameNotification,object: nil,queue: nil ) { (notification) in var userInfo = notification.userInfo! let frameEnd = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue let convertedFrameEnd = self.view.convertRect(frameEnd,fromView: nil) let heightOffset = self.view.bounds.size.height - convertedFrameEnd.origin.y self.messageFieldBottomConstraint.constant = heightOffset let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey]!.unsignedIntValue let options = UIViewAnimationOptions(rawValue: UInt(curve) << 16) UIView.animateWithDuration( userInfo[UIKeyboardAnimationDurationUserInfoKey]!.doubleValue,delay: 0,options: options,animations: { self.view.layoutIfNeeded() },completion: nil ) }
self.messageFieldBottomConstraint是一个NSLayoutConstraint,它将文本字段的底部绑定到视图的底部.当键盘出现时,此代码使该字段向上移动,当它消失时.
所有这一切都可能在iOS< 8通过使用UIKeyboardWillShowNotification和UIKeyboardWillHideNotification的组合.但!如您所说,iOS 8引入了可以由用户折叠或展开的QuickType部分.该解决方案将正确地动画文本字段,以使其始终附加到键盘的顶部,无论QuickType是否打开.