keyboard height varies when appearing

自古美人都是妖i 提交于 2020-08-22 05:29:19

问题


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.


回答1:


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




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/42358783/keyboard-height-varies-when-appearing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!