我已经实现了在键盘上方正确显示的自定义 inputAccessoryView。
问题是在屏幕底部我有 UIToolbar。当键盘关闭时,我的自定义 inputAccessoryView 停靠在屏幕底部并覆盖 UIToolBar。
关闭键盘时,有什么方法可以将 inputAccessoryView 停靠在 UIToolbar 上方?
我已经实现了在键盘上方正确显示的自定义 inputAccessoryView。
问题是在屏幕底部我有 UIToolbar。当键盘关闭时,我的自定义 inputAccessoryView 停靠在屏幕底部并覆盖 UIToolBar。
关闭键盘时,有什么方法可以将 inputAccessoryView 停靠在 UIToolbar 上方?
不容易,因为它inputAccessoryView
总是在您的视图上方,因为它位于不同的窗口中。它会带来很多并发症。
您是否尝试过在 self.view 层次结构中使用UIKeyboardWillShowNotification
和UIKeyboardWillHideNotification
使用附件视图?
首先在您的视图控制器中订阅通知
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)
然后也在你的视图控制器中
func keyboardWillShow(notification:NSNotification) {
self.transitionWithKeyboardInfo(notification.userInfo!)
}
func transitionWithKeyboardInfo(userInfo:NSDictionary) {
let durationNumber = userInfo[UIKeyboardAnimationDurationUserInfoKey]
let duration = durationNumber?.doubleValue
let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey]
let animationCurveRaw = animationCurveRawNSN?.unsignedLongValue ?? UIViewAnimationOptions.CurveEaseInOut.rawValue
let animationCurve = UIViewAnimationOptions(rawValue: animationCurveRaw)
let endFrameValue = userInfo[UIKeyboardFrameEndUserInfoKey]
let keyboardEndFrame = self.view.convertRect(endFrameValue!.CGRectValue, fromView:nil)
UIView.animateWithDuration(duration!, delay: 0.0, options: animationCurve, animations: {
// Now use keyboardEndFrame.size.height to move your view upwards
// you can find out if you're dismissing or showing and do different animations here
}, completion: nil)
}
不要忘记删除 dealloc / viewWillDisappear 中的通知观察
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
用于ScrollViewDidScroll
您的交互式附件视图移动...
var startingPoint:CGPoint
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
self.startingPoint = scrollView.panGestureRecognizer.locationInView(scrollView)
}
func scrollViewDidScroll(scrollView: UIScrollView) {
let currentPoint = scrollView.panGestureRecognizer.locationInView(scrollView)
let deltaY = currentPoint.y - self.startingPoint.y
}
如果您触摸到拦截附件视图框架,那应该是您需要解决的所有问题......然后你可以决定如何用键盘转换它......最简单的是谷歌环聊所做的并将附件视图转换为键盘作为独立的实体......它可能不是你想要的 100%,但过了一段时间我和它和平相处,让它看起来不错。