为 UITextField 弹出键盘时 UIView 消失 [英] UIView disappears when keyboard pops up for UITextField
问题描述
我在容器视图中有一个 UITextField
,当键盘弹出时容器视图消失.在同一个容器视图中有一个 UICollectionView
,它的每个自定义单元格都包含一个 UITextField
并且键盘对它们来说工作得很好.
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.
我在 keyboardWillShow
调用的动画函数中打印了容器视图的框架,并且容器视图的框架在两种情况下都是相同的,所以看起来容器视图刚刚消失(而不是我想的未移动")当特定的 UITextField
被选中时.相关代码是:
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)
})
}
可以在此线程中找到屏幕截图:KeyboardWillShow 只为某些 UITextFields 移动容器 UIView
the screenshots can be found in this thread: KeyboardWillShow only moves container UIView for certain UITextFields
在这一点上,我几乎可以肯定是自动布局约束困扰着我.
at this point I am almost certain that it is the auto layout constraints that are screwing with me.
推荐答案
尝试在每个动画事务结束时添加 commitAnimations :) 我也是我遇到了这个问题...现在它工作正常..你可以添加委托文本字段,您可以将其移动委托方法 DidBeginEditing 和 DidEndEditing 方法用于文本字段:)
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 :)
这篇关于为 UITextField 弹出键盘时 UIView 消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!