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.

Rajesh73
  • 342
  • 1
  • 6
  • 16
Gavin
  • 2,784
  • 6
  • 41
  • 78

3 Answers3

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

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
  • 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 `260` – Gavin Feb 21 '17 at 05:35
  • @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`. – dahiya_boy Feb 21 '17 at 05:48
  • 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. – Gavin Feb 21 '17 at 05:54
  • 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 `. – dahiya_boy Feb 21 '17 at 05:56
  • Yes, it returns me `302` which is incorrect. My view gets adjusted properly with `260` instead of `302`. `302` is too far up. – Gavin Feb 21 '17 at 06:00
  • @MaTaKazer Its working properly here. I also in a project doing same thing in making chat Layout. – dahiya_boy Feb 21 '17 at 06:11
7

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

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 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. – matt Feb 21 '17 at 05:22
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

MohyG
  • 1,335
  • 13
  • 25