UIView disappears when keyboard pops up for UIText

2019-07-14 07:46发布

问题:

I have an UITextField inside of a container view, when the keyboard pops up the container view disappears. In the same container view is a UICollectionView, whose custom cells each contains a UITextField and the keyboard works just fine for them.

I printed out the frame of the container view in the animation function that is called by keyboardWillShow and the container view's frames are the same for both cases, so it looks like the container view just disappears (instead of "not moved" as i thought) when that specific UITextField is selected. The relevant code is :

func keyboardWillShow(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        if let keyboardSize =  (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            kbHeight = keyboardSize.height
            self.animateDurationView(true)
        }
    }
}
func keyboardWillHide(notification: NSNotification) {

    self.animateDurationView(false)
}

func animateDurationView(up: Bool) {
    var movement = up ? -kbHeight : kbHeight
    println(movement)
    UIView.animateWithDuration(0.3, animations: {
        self.durationView.frame = CGRectOffset(self.durationView.frame, 0, movement)
        println(self.durationView.frame)
    })
}

the screenshots can be found in this thread: KeyboardWillShow only moves container UIView for certain UITextFields

EDIT: at this point I am almost certain that it is the auto layout constraints that are screwing with me.

回答1:

Try adding commitAnimations at the end of each animation transaction :) Me too I faced this issue... Now it is working fine.. You can add delegate to the textfield and you can move it delegate methods DidBeginEditing and DidEndEditing methods for textfield :)



回答2:

Just had the same issue. What I did wrong was trying to apply the movement to an inner view. Moving the outermost view made it work as this outer view does not have any auto layout constraints.

So to answer the question - if anyone else should get here Only move the self.view