swift如何在UIToolbar上面停靠inputAccessoryView(不到底部)

时间:2016-05-19 15:01:18

标签: ios swift inputaccessoryview

我已经实现了键盘上方正确显示的自定义inputAccessoryView。

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

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

1 个答案:

答案 0 :(得分:0)

不容易不行,因为inputAccessoryView将始终位于您的观看之上,因为它位于不同的窗口中。它会带来许多复杂情况。

您是否尝试在自我.view层次结构中将UIKeyboardWillShowNotificationUIKeyboardWillHideNotification与accessoryView一起使用?

首先在View Controller中订阅通知

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

然后也在你的View Controller中

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
}

如果您触摸的是拦截附件视图框架,那么应该是您需要解决的所有内容...然后您可以决定如何使用键盘进行转换...最简单的是Google环聊做什么和过渡作为独立实体的配件视图进入键盘...它可能不是你想要的100%,但过了一段时间我用它做了我的平静并使它看起来很好。