简体   繁体   English

UITextField,在辞退第一响应者时,导致奇怪的文本动画滚动

[英]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.

相关问题 UITextField不辞职第一响应者? - UITextField not resigning first responder? 辞去UITextField的第一个响应者时,NSParagraphStyle发生了变化 - NSParagraphStyle is changed when resigning UITextField's first responder 将文本视图退出为第一响应者,并在按下回车键时将文本字段设为第一响应者 - Resigning a text view as first responder and making a text field first responder when pressing return iOS文本字段未退出第一响应者 - iOS text field not resigning first responder 在文本字段中分配和辞退第一响应者 - Assigning and Resigning first responder in text field 退出急救人员状态时,UKeyboard“闪烁” - UKeyboard “flickers” when resigning first responder status 存储指针并退出第一响应者后,无法重新聚焦UITextField - Unable to refocus UITextField after storing pointer and resigning first responder 找出UITextfield的结束编辑,而无需辞职第一响应者 - Find out UITextfield's end editing without resigning first responder 辞职第一响应者和查看结束编辑方法导致应用程序崩溃 - Resigning First Responder and View End Editing methods causing app to crash 当 UITextField 成为第一响应者时,如何使 UIScrollView 自动滚动 - How to make a UIScrollView auto scroll when a UITextField becomes a first responder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM