3

I'm new to iOS. I've just taken over for another developer. I recently realised that this is a major issue because users cannot use our app. The keyboard gets dismissed as soon as it's shown. So no one can enter anything in the text field/s. It's happening only on iOS 15.4 and 15.4.1

This is the code that moves the entire view frame up/down when the keyboard is shown/dismissed (Apart from registering the observers and stuff). This is currently used in the app for around 2 years now.

override func viewWillAppear(_ animated: Bool) {
  registerForKeyboardNotifications()
}

func registerForKeyboardNotifications() {
  // UIResponser.keyboardWillShowNotification is supplemented 
  // with UIResponser.keyboardDidShowNotification only in the second gif image below
  NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        
  NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
        
}


@objc func keyboardWillShow(_ sender: Foundation.Notification) {
        
  let info = sender.userInfo!
        
  let keyboardFrame: CGRect = (info[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        
    UIView.animate(withDuration: 0.1, animations: { () -> Void in    
      if self.view.frame.origin.y == self.currentOrigin {
        self.view.frame.origin.y -= keyboardFrame.size.height
      }
    })
        
}
    
@objc func keyboardWillHide(_ sender: Foundation.Notification) {
        
  UIView.animate(withDuration: 0.1, animations: { () -> Void in
      self.view.frame.origin.y = self.currentOrigin
  })
        
}

override func viewWillDisappear(_ animated: Bool) {
  deregisterFromKeyboardNotifications()
}

func deregisterFromKeyboardNotifications(){
        
  NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
        
  NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
        
}

Is there any other way to handle this for iOS 15.4? The above code is mostly what I find online but it doesn't seem to be an option.

iOS 15.4 with the above code

iOS 15.4 with the above code

iOS 15.4 with the above code but the keyboardWillShow function above being triggered by the keyboardDidShow observer instead

iOS 15.4 with the above code but the keyboardWillShow function above being triggered by the keyboardDidShow observer instead

iOS 15.4 with the above code commented out (Basically not moving the view/s up/down when detecting keyboard show/hide)

iOS 15.4 with the above code commented out

iOS 15.2 (and all lower versions) with the above code working perfectly as intended

iOS 15.2 with the above code working perfectly as intended

Devenom
  • 915
  • 1
  • 9
  • 20
  • What is your evidence that the keyboard disappears, and what does that have to do with the code you've shown? – matt Apr 17 '22 at 20:42
  • 1
    if you can attach some video then we might help you – keshav Apr 18 '22 at 04:12
  • may be it should be linked with any third party library try updating them if you are using them for keyboard – keshav Apr 18 '22 at 04:14
  • @matt I've been debugging it using print statements. It does go to the keyboardWillShow function and immediately to the keyboardWillHide function. – Devenom Apr 18 '22 at 08:11
  • @keshav There's no third party library currently present within our app. I wanted to make a video but it's an app for a company so I'm not sure if I can do so. I'll try changing the UI contents and add one – Devenom Apr 18 '22 at 08:13
  • @matt also the above code is the one that works on all iOS versions except 15.4 and 15.4.1. if I change the observer from keyboardWillShow to keyboardDidShow I even visually get to see the keyboard for a second – Devenom Apr 18 '22 at 08:21
  • "It does go to the keyboardWillShow function and immediately to the keyboardWillHide function." But that merely shows call order; post hoc is not propter hoc. I'm asking what makes you think that the `keyboardWillShow` function implementation somehow _causes_ the `keyboardWillHide`to be called immediately. (And I am hinting to you that, when you test properly, you will find it does not — which means you are looking in totally the wrong place.) – matt Apr 18 '22 at 14:51
  • @matt You got me completely wrong. I never said that the keyboardWillShow function implementation has anything to do with the keyboardWillHide. I'm saying that these two functions are implemented as they are above and work fine on other iOS versions. I don't know what's causing the keyboardWillHide function to be called immediately but it does on ios 15.4 and 15.4.1 – Devenom Apr 18 '22 at 19:34
  • If I comment out the code in these two functions the keyboard does show but it shows over the layout hiding the views we want visible. – Devenom Apr 18 '22 at 19:36
  • 2
    @Devenom if you create a separate repo or a gist that users can use to then replicate this bug, it might help in resolving it or maybe add some more code that would be insightful. – Jude Fernandes Apr 20 '22 at 13:06
  • Done. I added all the code and images above. – Devenom Apr 20 '22 at 18:03

1 Answers1

1

Try to add constraints like this

contentView.bottomAnchor.constraint(equalTo: view.keyboardLayoutGuide.topAnchor)

or check this link

also video tutorial here