gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:13 25 4
gpt4 key购买 nike

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

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

有什么方法可以在键盘关闭时将 inputAccessoryView 停靠在 UIToolbar 上方?

最佳答案

不是很容易,因为 inputAccessoryView 将始终位于您的 View 上方,因为它位于不同的窗口中。它会带来很多并发症。

您是否尝试过在您的 self.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 进行交互式附件 View 移动...

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
}

如果您的触摸正在拦截 accessoryView 框架,那么这应该是您需要解决的所有问题...然后您可以决定如何使用键盘转换它...最简单的是 Google Hangouts 所做的并转换附件将键盘作为单独的实体查看...这可能不是您想要的 100%,但过了一会儿我与它和平相处并让它看起来不错。

关于ios - swift 如何将 inputAccessoryView 停靠在 UIToolbar 上方(而不是底部),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37327116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com