keyboardWillShowNotification在被解雇时调用两次

时间:2018-06-11 07:06:31

标签: ios iphone swift ios12

我已订阅keyboardWillShowNotificationkeyboardWillHideNotification来移动我的用户界面。我注意到当我通过点击“开始”按钮关闭键盘时,keyboardWillShowNotification被调用两次(因此重置了我的一些约束)但是如果通过击中键盘上的返回(MacBook)而关闭,那么它就不会被调用两次。

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

这是我的代码:

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)

    }

1 个答案:

答案 0 :(得分:0)

我还没有解决在iOS键盘模拟器上点击返回时发布的keyboardWillShowNotification的问题,而不是在模拟器中的硬件键盘上,但我修改了我的代码,以便在显示键盘时我不会#39; t添加约束,我只需使用键盘通知的高度修改约束的常量。这解决了它。

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