Swift iOS11 - keyboard height detection doesn't work anymore

I just noticed that my keyboard height ID no longer works with iOS11.

For iOS10 devices, I used this logic to detect if the keyboard hides a specific input field (in my case, a text field). If so, the keyboard will be displayed below the last active text field to correctly enter users.

In case of iOS 11, keyboard height identification does not work.

WillAppear logic keyboard helper class example Here's just an example of what keyBoardWillShow does → It just checks to see if you need to move the view above the keyboard if the keyboard hides the text box.

I did some debuggin and found out that the line of code below works differently between iOS 10 and iOS 11:

if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue 

debugger output iOS10

keyboardSize CGRect (start = (x = 0, y = 568), size = (width = 320, height = 216))

iOS11 debugger output

keyboardSize CGRect (start = (x = 0, y = 568), size = (width = 320, height = 0))

Below you can see the full code - it worked before iOS 10.3

 func keyboardWillShow(notification: NSNotification, view: UIView, activeTextField: UITextField?, scrollView: UIScrollView?) { if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { if view.frame.origin.y == 0{ var aRect : CGRect = (view.viewWithTag(2)?.frame)! aRect.size.height -= keyboardSize.height if let activeField = activeTextField { let tempPoint = CGPoint(x: activeField.frame.origin.x, y: activeField.frame.origin.y + 20) if (aRect.size.height < tempPoint.y){ view.frame.origin.y -= keyboardSize.height if let scrollView = scrollView { let bottomOffset = CGPoint(x: 0, y: scrollView.contentSize.height - scrollView.bounds.size.height) scrollView.setContentOffset(bottomOffset, animated: true) } } } } } } 

UPDATE 2017/09/20

I have tried this several times. Sometimes it also shows me the keyboard height value for iOS11 - now I'm completely confused .....

+5
source share
1 answer

Use UIKeyboardFrameEndUserInfoKey instead of UIKeyboardFrameBeginUserInfoKey

+10
source

Source: https://habr.com/ru/post/1271990/


All Articles