[英]UITextField, when resigning first responder, causing strange animation scroll of text
I've got a couple of UITextFields implemented in a UITableView for a login form. 我在UITableView中为登录表单实现了几个UITextFields。 When resigning first responder in both the very first time , a really strange animation jump is occurring.
两次都辞退第一响应者时 ,发生了一个非常奇怪的动画跳跃。 Since these are almost entirely build in Interface Builder with a .xib file, I've got virtually no code to add in. But here's a fun .gif that shows the behavior:
由于这些几乎完全是使用.xib文件在Interface Builder中构建的,因此实际上没有要添加的代码。但这是一个有趣的.gif,它显示了该行为:
Update: 更新:
I've narrowed it down to the fact that I'm listening to keyboard events to adjust the view constraints. 我将其范围缩小到我正在监听键盘事件以调整视图约束的事实。 This is the code that's causing the problem:
这是导致问题的代码:
func keyboardWillHide(notification: NSNotification) {
// tried self.formContainer.layoutIfNeeded() here too to force pending layouts
formContainerYConstraint.constant = 40
UIView.animateWithDuration(0.4) { () -> Void in
self.formContainer.layoutIfNeeded()
}
}
... where the form container is a view that houses the table view and login button. ...,其中表单容器是一个包含表格视图和登录按钮的视图。
Feels like a total hack (and I'd love for someone to post a better answer) but in the mean time, I've resolved this by adding a slight delay to the animation action - I suspect this is related to the become- and resignFirstResponder events occurring when switching between two input fields. 感觉就像是一堆骇客(我希望有人能提供更好的答案),但与此同时,我通过在动画动作上稍加延迟来解决了这一问题-我怀疑这与在两个输入字段之间切换时发生resignFirstResponder事件。
let delay: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC)))
dispatch_after(delay, dispatch_get_main_queue()) { () -> Void in
self.formContainerYConstraint.constant = 40
UIView.animateWithDuration(0.4) { () -> Void in
self.formContainer.layoutIfNeeded()
}
}
Try this 尝试这个
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[textField layoutIfNeeded];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.