keyboard height varies when appearing
Asked Answered
A

3

8

I am trying to bring my views up when the keyboard appears by modifying the bottom constrain to the keyboard height. But the keyboard height returned to me is varying.

When I tap on a textfield in the simulator, the keyboard height was 302. When I try to toggle on and off the software keyboard, it shows 260 when the keyboard appears. Why is this happening?

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(FriendsViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)

func keyboardWillShow(notification: NSNotification) {
    print("Keyboard appearing")
    guard let keyboardHeight = (notification.userInfo! as NSDictionary).objectForKey(UIKeyboardFrameBeginUserInfoKey)?.CGRectValue.size.height else {
        return
    }
    bottomConstraint.constant = keyboardHeight
    print("keyboard height : \(keyboardHeight)")
    self.view.layoutIfNeeded()
}

Height of 260 is actually the correct height, as it adjusted my views perfectly. With a height of 302 my views get offset too far up.

The layout of my view is. UITextField on the top and followed by a UITableView below it.

Ayannaaycock answered 21/2, 2017 at 4:37 Comment(5)
Which device you are using?Prady
Simulating on iPhone 7Ayannaaycock
OK let me check your code here, wait for while.Prady
Yeah there is height difference. iOs unable to predicate the correct height.Prady
@MaTaKazer https://mcmap.net/q/153318/-uitableview-and-uiview-with-keyboardwillshowBleak
P
18

Modified answer of Matt with reason,

He is right you need to use UIKeyboardFrameEndUserInfoKey instead of UIKeyboardFrameBeginUserInfoKey because

  1. UIKeyboardFrameEndUserInfoKey gives you the final height according the prefences you have set in your setting.

  2. UIKeyboardFrameEndUserInfoKey returns you height two times first one is without the language prediction as you can see above the keyboard and next one with predicate if it is activated from the setting but UIKeyboardFrameBeginUserInfoKey returns without prediction bar.

Height on toggle in iPhone 5s

enter image description here

Prady answered 21/2, 2017 at 5:26 Comment(6)
When I tap on the UITextField it is still showing the larger height of 302 on the iPhone 7. It is only when I toggle on and off the software keyboard did it managed to show the correct height of 260Ayannaaycock
@MaTaKazer See the image I added in answer , it is the output I got on when I toggle software keyboard. I checked in 5s thats why its 216 & 253 not 260 & 302.Prady
Yes, I get the 2 values when I toggle the software keyboard in the simulator. But in reality a user is gonna tap on the UITextField which returns me the wrong height.Ayannaaycock
No No bro! that one is the correct height. Both height must be same in your case its must be 302. Ensure : you are using UIKeyboardFrameEndUserInfoKey .Prady
Yes, it returns me 302 which is incorrect. My view gets adjusted properly with 260 instead of 302. 302 is too far up.Ayannaaycock
@MaTaKazer Its working properly here. I also in a project doing same thing in making chat Layout.Prady
A
7

The problem is that you are looking at UIKeyboardFrameBeginUserInfoKey. What you want to look at is UIKeyboardFrameEndUserInfoKey.

Attune answered 21/2, 2017 at 5:11 Comment(1)
And there are other problems with your code. You need to convert from window coordinates to local coordinates, plus you need to make no assumptions about the keyboard height — look at the position of the keyboard top instead.Attune
S
0

this is how I did in swift 2 first add this function:

    // Lifting the view up
func animateViewMoving (up:Bool, moveValue :CGFloat){
    let movementDuration:NSTimeInterval = 0.3
    let movement:CGFloat = ( up ? -moveValue : moveValue)
    UIView.beginAnimations( "animateView", context: nil)
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.setAnimationDuration(movementDuration )
    self.view.frame = CGRectOffset(self.view.frame, 0,  movement)
    UIView.commitAnimations()
}

and then implement UITextFieldDelegate :

    // MARK: - UITextFieldDelegate

func textFieldShouldReturn(textField: UITextField) -> Bool {
    // Hide the keyboard.
    textField.resignFirstResponder()
    return true
}

func textFieldDidEndEditing(textField: UITextField) {
    animateViewMoving(false, moveValue: 100)
    textField.resignFirstResponder()
}

func textFieldDidBeginEditing(textField: UITextField) {
    animateViewMoving(true, moveValue: 100)
}

you can also get the exact height of keyboard like this :

var viewLiftUpValue : CGFloat

func keyboardWillShow(notification:NSNotification) {

        let userInfo:NSDictionary = notification.userInfo!
        let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
        let keyboardRectangle = keyboardFrame.CGRectValue()
        let keyboardHeight = keyboardRectangle.height
        viewLiftUpValue = keyboardHeight
    }

and then give it to animateViewMoving() function

Salesperson answered 21/2, 2017 at 7:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.