微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

即使键盘完全可见并且再次点击焦点文本视图,也会调用 KeyboardWillChangeFrame 通知 编辑

如何解决即使键盘完全可见并且再次点击焦点文本视图,也会调用 KeyboardWillChangeFrame 通知 编辑

键盘开始出现时,我正在向上移动我的文本视图。但是即使键盘完全可见,这个方法也会被触发,并且用户再次点击文本视图。因此,表格视图偏移量不断增加内容不断移动到屏幕外。

类似问题:UITextField UIKeyboardWillShowNotification sent for every UITextField on a screen in iOS9 even while keyboard is visible

我正在使用 pod KeyboardObserver(为 Swift 5 更新)。它本质上与 NotificationCenter.default.addobserver(self,selector: #selector(handleKeyboardWillChangeFrame(_:)),name: NSNotification.Name.UIKeyboardWillChangeFrame,object: nil) 做同样的事情。

      keyboard.observe { [weak self] (event) -> Void in
                guard let self = self else { return }
                switch event.type {
                case .willChangeFrame:
                    self.handleKeyboardWillChangeFrame(keyboardEvent: event)
//                case .willShow,.willHide:
//                    self.handleKeyboardWillChangeFrame(keyboardEvent: event)
                default:
                    break
                }
            }

听众:

func handleKeyboardWillChangeFrame(keyboardEvent: KeyboardEvent) {
          
            
            let uiScreenHeight = UIScreen.main.bounds.size.height
            let endFrame = keyboardEvent.keyboardFrameEnd
            
            let endFrameY = endFrame.origin.y
            
            let offset = -1 * endFrame.size.height
            
            print("Handling keyboard change frame:  End Y - ",endFrameY)
            
            if endFrameY >= uiScreenHeight {
                self.discussionsMessageBoxBottomAnchor.constant = 0.0
                self.discussionChatView.discussionTableView.contentOffset.y += 2 * offset
            } else {
                self.discussionsMessageBoxBottomAnchor.constant = offset
                self.discussionChatView.discussionTableView.contentOffset.y -= offset

            }
            
            UIView.animate(
                withDuration: keyboardEvent.duration,delay: TimeInterval(0),options: keyboardEvent.options,animations: {
                    self.view.layoutIfNeeded()
                    
                },completion: nil)
        }

控制台输出

Handling keyboard change frame:  End Y -  737.0
Handling keyboard change frame:  End Y -  521.0
Handling keyboard change frame:  End Y -  479.0
Handling keyboard change frame:  End Y -  479.0 <-- Keyboard visible and TextView tapped
Handling keyboard change frame:  End Y -  479.0 <-- Keyboard visible and TextView tapped
Handling keyboard change frame:  End Y -  479.0 <-- Keyboard hiding
Handling keyboard change frame:  End Y -  812.0 <-- Keyboard hidden

编辑

这是我目前的解决方法,但我仍然想知道这里的基本问题是什么。

class discussionsViewController: UIViewController {


...


var oldKeyboardEndFrameY: CGFloat = 0


...


func handleKeyboardWillChangeFrame(keyboardEvent: KeyboardEvent) {
          
            let uiScreenHeight = UIScreen.main.bounds.size.height
            let endFrame = keyboardEvent.keyboardFrameEnd
            let endFrameY = endFrame.origin.y
            
            if oldKeyboardEndFrameY == endFrameY {
                return
            }
            oldKeyboardEndFrameY = endFrameY
            
            let offset = -1 * endFrame.size.height
            
            print("Handling keyboard change frame:  End Y - ",endFrameY)
            
            if endFrameY >= uiScreenHeight {
                self.discussionsMessageBoxBottomAnchor.constant = 0.0
                self.discussionChatView.discussionTableView.contentOffset.y += 2 * offset
            } else {
                self.discussionsMessageBoxBottomAnchor.constant = offset
                self.discussionChatView.discussionTableView.contentOffset.y -= offset
            }
            
            UIView.animate(
                withDuration: keyboardEvent.duration,completion: nil)
        }

...


}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。