【问题标题】:keyboardWillShowNotification called twice when dismissedkeyboardWillShowNotification 被解雇时调用了两次
【发布时间】:2018-06-12 19:07:06
【问题描述】:

我已订阅 keyboardWillShowNotificationkeyboardWillHideNotification 以在我的 UI 中移动。我注意到,当我通过点击“开始”按钮关闭键盘时,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)

    }

【问题讨论】:

  • 显示您如何订阅通知以及在哪里订阅?
  • 我已将观察者添加到 viewDidLoad - 我也尝试了 viewDidAppear 但没有改变
  • 在调用 super.viewWillAppear() 后尝试在 viewWillAppear() 中注册
  • 你找到解决办法了吗?
  • 见下面我的回答。我为每个通知添加了约束,我通过简单地更改现有的来修复它。

标签: ios iphone swift ios12


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-15
    • 2013-05-22
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    相关资源
    最近更新 更多