The firing order of NotificationCenter observing when the keyboard appears and textViewDidBeginEditing has been changed.

If you are seeing the same thing please reply to this post.

The firing order was changed when tapping a textView or textField. I first noticed this in iOS 15.6.1 on my physical device. If I send the app to a physical device running iOS 15.5 everything worked fine. Once I updated the device to iOS 15.6.1 or higher the change in firing order broke the ability to get the keyboard height when the textView was first tapped. It's happening on both iPad and iPhone.

In Xcode 13.4.1, iOS 15.5 and before. keyboardWillShow fired then textViewDidBeginEditing fired after. This allowed to get the keyboard height before the textView became active and thus move the textView up the proper amount.

In Xcode 14.0.1, or anything above iOS 15.5. textViewDidBeginEditing fires then keyboardWillShow fires after.

So if you're moving the textview up to clear the keyboard it won't work right anymore because you won't get the keyboard height until after the textView has become firstResponder.

The firing order was also changed for textFields.

I thought about hard-coding in the keyboard heights for each device but that really doesn't make sense. That and what if Apple changes the keyboard height.

I have filed a bug report but so far no response. If you're using notificationCenter to get the keyboard height please check if your app is broken too. If so, please file a bug report so we can get some traction. I have several apps I'm waiting to release and this is holding me up.

You can use the code below to check the before and after firing order. Keep in mind that the textView delegate needs to be the view.

In viewDidLoad I have this.

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)

The selector for NotificationCenter.default.addObserver

 // MARK: - get the keyboard height
    @objc func keyboardWillShow(notification: NSNotification)
    {
        if let keyboardRectValue = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
        {
            print("keyboardHeight ran \(keyboardHeight)")
        }
    }

Then I have:

 // MARK: - TextView Editing Begin.
    func textViewDidBeginEditing(_ textView: UITextView)
    {
        print("textViewDidBeginEditing ran")
    }
Post not yet marked as solved Up vote post of quailcreek Down vote post of quailcreek
746 views

Replies

Sounds like an annoying change, but was the old behavior (the relative order of the notification and delegate call) actually documented? If not, then I wouldn’t be surprised to hear back from Apple that this isn’t a bug.

Following more testing it seems the firing order is changing with each iOS release.

Can somebody please verify this?

iOS 15.5 keyboardWillShow textViewDidBeginEditing

iOS 15.6.1 textViewDidBeginEditing keyboardWillShow

iOS 15.7 keyboardWillShow textViewDidBeginEditing

iOS 16 - in the sim because I'm reluctant to update to 16 on my devices. textViewDidBeginEditing keyboardWillShow

  • Did you check it already on physical device? I had a similar issue, which was reproducable on simulator with iOS 16.0, but works on a (same model) physical device with iOS 16.0.2.

Add a Comment