问题描述
我想知道键盘是否完全可见,而不是它是否处于演示状态之间。不幸的是,所有答案都使用 UIKeyboardDidShow
、UIKeyboardWillShow
或 UIKeyboardWillChangeFrame
来检查键盘是否可见。当键盘出现时,所有这些都会多次触发侦听器。所以我不能使用它们中的任何一个来明确判断键盘是否完全可见。我想知道键盘是否完全可见并阻止在我的 UIKeyboardWillShow
侦听器中触发操作。
let keyBoardHeight: CGFloat = 0
NotificationCenter.default.addobserver(self,selector: #selector(handleKeyboardWillChangeFrame),name: NSNotification.Name.UIKeyboardWillChangeFrame,object: nil)
@objc func handleKeyboardWillChangeFrame(notification: NSNotification) {
print("Keyboard notif called")
guard let userInfo = notification.userInfo else { return }
let uiScreenHeight = UIScreen.main.bounds.size.height
let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
let endFrameY = endFrame?.origin.y ?? 0
let duration:TimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIView.Animationoptions.curveEaseInOut.rawValue
let animationCurve:UIView.Animationoptions = UIView.Animationoptions(rawValue: animationCurveRaw)
let offset = -1 * (endFrame?.size.height ?? 0.0)
print("Keyboard Height: ",keyBoardHeight," End Frame Height: ",endFrame?.height ?? 0)
if endFrameY >= uiScreenHeight {
self.discussionsMessageBoxBottomAnchor.constant = 0.0
discussionChatView.discussionTableView.contentOffset.y += 2 * offset
} else
if keyBoardHeight != endFrame?.height ?? 0 // Extra check added to avoid triggering this part when keyboard is already fully visible
{
//Once the keyboard is fully visible,any triggers of this method will find keyBoardHeight = endFrame?.height. What ends up happening is once the keyboard is fully shown,if the user again taps on the text view,this function gets triggered and execution enters the else part
self.discussionsMessageBoxBottomAnchor.constant = offset
discussionChatView.discussionTableView.contentOffset.y -= offset
}
keyBoardHeight = max(keyBoardHeight,endFrame?.height ?? 0)
UIView.animate(
withDuration: duration,delay: TimeInterval(0),options: animationCurve,animations: { self.view.layoutIfNeeded() },completion: nil)
}
不幸的是,这并没有产生预期的结果。这就是为什么我正在寻找任何其他方式来告诉我键盘是否完全可见。这将阻止键盘外观处理代码不必要地添加更多偏移以适应已经可见的键盘
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)