Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift iOS11 - identify keyboardSize height doesn't work anymore

I just noticed that my keyboard height identification code is not working anymore with iOS11.

For iOS10 devices I used this logic to detect, if a keyboard will hide a specific input field (in my case a text field). If thats the case, the keyboard will be displayed under the last active textfield to enable the users input properly.

In case of iOS 11 the identification of keyboard height doesn't work.

Helper class example for keyboard willAppear logic Here is just an example what keyBoardWillShow does -> It just checks, if the view needs to be shifted above the keyboard, if the keyboard is going to hide the textfield.

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

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

iOS10 debugger output

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

iOS11 debugger output

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

Below you can see the full code - it worked until 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 tried it now several times. Sometimes it it shows me also a keyboard height value for iOS11 - now I'm totally confused.....

like image 661
AlexWoe89 Avatar asked Sep 20 '17 15:09

AlexWoe89


1 Answers

Use UIKeyboardFrameEndUserInfoKey instead of UIKeyboardFrameBeginUserInfoKey

like image 52
irmu Avatar answered Nov 15 '22 12:11

irmu