代码之家  ›  专栏  ›  技术社区  ›  Pranav Wadhwa

键盘将显示不称为iOS 12的通知

  •  1
  • Pranav Wadhwa  · 技术社区  · 6 年前

    在我的应用程序中,我想要一个通知 UIResponder.keyboardWillShowNotification

    @objc func keyboardWillShow(_ notification: Notification) {
        print("keyboard will show 2")
        guard let frameValue: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else {
            return
        }
        let keyboardFrame = frameValue.cgRectValue
        UIView.animate(withDuration: animationTime) {
            self.addViewBottomConstraint.constant = keyboardFrame.size.height
            self.view.layoutIfNeeded()
            print("Bottom contraint height = \(self.addViewBottomConstraint.constant)")
        }
    }
    
    @objc func keyboardWillHide(_ notification: Notification) {
        UIView.animate(withDuration: animationTime) {
            self.addViewBottomConstraint.constant = 0
            self.view.layoutIfNeeded()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    }
    

    在这里,“keyboard will show 2”不打印,但为具有相同通知的其他视图控制器打印。iOS 12中有什么新的东西导致了这一点吗?否则,有没有一个特别的原因不叫它?谢谢你的帮助。

    0 回复  |  直到 6 年前