gpt4 book ai didi

ios8 - 仅使用 UIKeyboardWillChangeFrameNotification 通知

转载 作者:行者123 更新时间:2023-12-04 00:04:28 26 4
gpt4 key购买 nike

考虑到键盘的新 QuickType 部分。

可以使用 是真的吗?仅限 UIKeyboardWillChangeFrameNotification 的通知,

并且只是“不打扰”“旧” UIKeyboardWillShowNotification 和 UIKeyboardWillHideNotification ?

使用 测试似乎表明它运行良好仅限 keyboardFrameDidChange - 但我们可能遗漏了什么?

顺便说一句,这是一个如何使用 UIKeyboardWillChangeFrameNotification https://stackoverflow.com/a/26226732/294884 的示例

最佳答案

于 2021-05-17 为 Swift 5 更新
这绝对是可能的,并且可以将您的代码减少一半左右。下面的示例使用自动布局来完成很多繁重的工作。

NotificationCenter.default.addObserver(
forName: UIResponder.keyboardWillChangeFrameNotification,
object: nil,
queue: nil
) { (notification) in
guard let userInfo = notification.userInfo,
let frameEnd = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect,
let curveRaw = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? Int,
let curve = UIView.AnimationCurve(rawValue: curveRaw),
let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval
else { return }

// Convert the frame rects you're interested in to a common coordinate space
let keyboardRect = self.view.convert(frameEnd, from: nil)
let formContainerRect = self.view.convert(self.formContainerView.frame, from: nil)

// Calculate their intersection and adjust your constraints accordingly
let intersection = keyboardRect.intersection(formContainerRect)
if !intersection.isNull {
// Some overlap; adjust the bottom of your views (what you do here will vary)
self.formContainerBottomConstraint?.constant = intersection.size.height
} else {
// No overlap; reset your views to its default position
self.formContainerBottomConstraint?.constant = 0
}

let animator = UIViewPropertyAnimator.init(duration: duration, curve: curve) {
self.view.layoutIfNeeded()
}
animator.startAnimation()
}
self.formContainerBottomConstraintNSLayoutConstraint将我的(想象的)表单的底部绑定(bind)到我的 View 的底部。此代码在键盘出现时向上动画该字段,在键盘消失时向下动画。
通过使用 UIKeyboardWillShowNotification 的组合,所有这些都可以在 iOS < 8 中实现。和 UIKeyboardWillHideNotification .但!正如你所说,iOS 8 引入了 QuickType 部分,可以由用户折叠或展开。这个解决方案是 super 通用的,可以让你的应用程序响应操作系统给你的任何键盘变化。

关于ios8 - 仅使用 UIKeyboardWillChangeFrameNotification 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26316597/

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