Ios swift如何将inputAccessoryView停靠在UIToolbar上方(而不是底部)

Ios swift如何将inputAccessoryView停靠在UIToolbar上方(而不是底部),ios,swift,inputaccessoryview,Ios,Swift,Inputaccessoryview,我已经实现了自定义inputAccessoryView,当它出现时,它会正确地显示在键盘上方 问题是在屏幕的底部有UIToolbar。 当键盘关闭时,我的自定义inputAccessoryView停靠在屏幕底部并覆盖UIToolBar 当键盘关闭时,有没有办法将inputAccessoryView停靠在UIToolbar上方?不容易,因为inputAccessoryView将始终位于您的视图上方,因为它位于不同的窗口中。这会带来很多复杂的情况 您是否尝试过将UIKeyboardWillShowN

我已经实现了自定义inputAccessoryView,当它出现时,它会正确地显示在键盘上方

问题是在屏幕的底部有UIToolbar。 当键盘关闭时,我的自定义inputAccessoryView停靠在屏幕底部并覆盖UIToolBar


当键盘关闭时,有没有办法将inputAccessoryView停靠在UIToolbar上方?

不容易,因为
inputAccessoryView
将始终位于您的视图上方,因为它位于不同的窗口中。这会带来很多复杂的情况

您是否尝试过将
UIKeyboardWillShowNotification
UIKeyboardWillHideNotification
与self.view层次结构中的accessoryView一起使用

首先在视图控制器中订阅通知

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)
}
别忘了删除dealoc/viewWillEnglish中的通知观察

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
}

如果你是touch正在拦截accessoryView框架,那么这就是你需要解决的所有问题。。。然后你可以决定如何用键盘转换它。。。最简单的方法是Google Hangouts所做的,将附件视图作为单独的实体转换到键盘上。。。这可能不是你想要的100%,但过了一段时间,我平静下来,让它看起来不错

您可以编辑您的问题以显示代码吗?这是因为我还需要滑动键盘(self.tableView.keyboardismissmode=.Interactive),在这种情况下,我的inputAccessoryView应该在键盘向下滑动时跟随键盘。我想让inputAccessoryView首先跟随键盘,然后在到达uitoolbar时使inputAccessoryView保持在uitoolbar上方这仍然可以完成,但它相当复杂。使用
tableView
/
scrollView
/的
ScrollIndicationSets
contentInset
向下拖动,使用
scrollViewDidScroll
移动
accessoryView
您会发现它只在触摸键盘时移动,因此,您需要检测您何时触摸了附件视图,并开始该过程,然后如果这是不可接受的。以Google Hangouts为例。Magoo,你知道,我被卡住了:(
scrollViewDidScroll
功能工作得很好,但我无法检测到我的附件视图何时被触摸。我尝试使用
TouchsMoved(触摸:设置,withEvent…)
功能,但在我看来它现在不适用于
UIViewController
。我也尝试过
UIAppgestureRecognitizer
,但它会在触摸结束时为我提供坐标,而不是正确的坐标。有没有办法知道触摸了附件视图?然后我如何自己移动附件视图?(据我所知,附件视图会根据键盘的移动自动移动)正如我现在在函数
scrollViewDidScroll
scrollView中看到的那样。contentOffset
不是手指移动的真实点。它们是一些滚动偏移。我说得对吗?因此我的视图移动速度不如键盘移动速度快。我还尝试了
UITapgestureRecognitor
,效果很好-显示了r我按下并移动,但现在的问题是交互式键盘关闭模式根本不起作用-我可以移动键盘上方的视图(我现在使用的是公共视图,而不是附件视图),但当我将手指移到键盘上方时,键盘不会移动:(这是一项极其复杂的任务:所以不要放弃!但我找到了一种方法。使用
scrollView将开始搜索
并存储
CGPoint
self.startingPoint=[scrollView.PangestureRecognitizer locationInView:scrollView];
同样,在
scrollViewDidScroll
中使用
CGPoint currentPoint=[scrollView.PangestureRecognitizer locationInView:scrollView];
将为您获取当前点…然后您可以执行
currentPoint.x-self.startingPoint.x
currentPoint.y-self.startingPoint.y
以获取增量更改:)