ios - keyboardWillShowNotification 在被解雇时调用了两次

标签 ios iphone swift ios12

我已订阅 keyboardWillShowNotificationkeyboardWillHideNotification 以在我的 UI 中移动。我注意到,当我通过点击“Go”按钮关闭键盘时,keyboardWillShowNotification 被调用两次(因此重置了我的一些约束)但是如果通过点击键盘上的 return 关闭键盘(MacBook)然后它没有被调用两次。

如何避免它被调用两次?为什么这种行为甚至存在?我找不到任何关于它的提及(很多关于它被输入 View 调用两次的引用......等)但在被解雇时从来没有。

这是我的代码:

override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardNotification(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWasDismissed(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil) 
}

和...

@objc func keyboardNotification(notification: NSNotification) {
        guard
            let animationDuration = notification.userInfo?["UIKeyboardAnimationDurationUserInfoKey"] as? Double,
            let animationCurve = notification.userInfo?["UIKeyboardAnimationCurveUserInfoKey"] as? NSNumber,
            let frameEnd = notification.userInfo?["UIKeyboardFrameEndUserInfoKey"] as? CGRect,
            let frameBegin = notification.userInfo?["UIKeyboardFrameBeginUserInfoKey"]
            else {
                print("No userInfo recived from NSNotification.Name.UIKeyboardWillShow")
                return
        }
        print("WILL SHOW")

        let margin = self.view.safeAreaLayoutGuide
        constraintsWhenKeyboardVisible = [
                            boxOffice.leadingAnchor.constraint(equalTo: margin.leadingAnchor),
                            boxOffice.trailingAnchor.constraint(equalTo: margin.trailingAnchor),
                            boxOffice.bottomAnchor.constraint(equalTo: margin.bottomAnchor),
                            boxOffice.topAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -(frameEnd.height + 50))
                        ]
        NSLayoutConstraint.deactivate(boxOfficeFinalConstraints)
        NSLayoutConstraint.activate(constraintsWhenKeyboardVisible)

        UIView.animate(withDuration: animationDuration,
                       delay: TimeInterval(0),
                       options: UIView.AnimationOptions(rawValue: animationCurve.uintValue),
                       animations: {
                        self.boxOffice.answerField.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
                        self.view.layoutIfNeeded()
        },
                       completion: nil)
    }


@objc func keyboardWasDismissed(notification: NSNotification) {
        guard
            let animationDuration = notification.userInfo?["UIKeyboardAnimationDurationUserInfoKey"] as? Double,
            let animationCurve = notification.userInfo?["UIKeyboardAnimationCurveUserInfoKey"] as? NSNumber
            else {
                print("No userInfo recived from NSNotification.Name.UIKeyboardWillShow")
                return
        }
        print("WILL HIDE")
        //print(notification)
        NSLayoutConstraint.deactivate(self.constraintsWhenKeyboardVisible)
        NSLayoutConstraint.activate(self.boxOfficeFinalConstraints)

        UIView.animate(withDuration: animationDuration,
                       delay: TimeInterval(0),
                       options: UIView.AnimationOptions(rawValue: animationCurve.uintValue),
                       animations: {
                        self.boxOffice.answerField.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMaxXMinYCorner, .layerMinXMaxYCorner, .layerMinXMinYCorner]
                        self.view.layoutIfNeeded()
        },
                       completion: nil)

    }

最佳答案

我还没有解决 keyboardWillShowNotification 在 iOS 键盘模拟器上按下 Return 而不是在模拟器的硬件键盘上被发布的问题,但我已经修改了我的代码,以便在显示键盘时我不会添加约束,我只是使用键盘通知中的高度修改约束的常量。这已经解决了。

boxOfficeWhenKeyboardVisible[3].constant = -(frameEnd.height + 50)

关于ios - keyboardWillShowNotification 在被解雇时调用了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50792104/

相关文章:

iphone - 想使用 popViewControllerAnimated 而不是 popToRootViewControllerAnimated

iphone - 如何在Three20中添加json模块

swift - 无法设置 maprect 以显示所有注释

ios - 隐藏后更改 UIButton 标题

ios - 使用isNode(_:insideFrustumOf :)与nodesInsideFrustum(of :)时的结果不同

ios - 如何以编程方式在 Spritekit 中截取屏幕截图?

iphone - FBWebDialog 在 iOS 7 中出现时消失

ios - 如何根据文本使 UICollectioncell 宽度动态

objective-c - 将数据传递给 UINavigationController Partent

iphone - RestKit:请求失败时获取HTTP状态码