iOS 9 UITextField textRectForBounds问题

Rei*_*ner 3 cocoa-touch uitextfield ios9

我在iOS 9,XCode 7 GM上运行.我有一个扩展UITextField(CustomOffsetTextField)的类,并提供对自定义文本定位的支持,如下所示:

override func textRectForBounds(bounds: CGRect) -> CGRect {
    return CGRectOffset(bounds, textOffset.x + leftViewOffset, textOffset.y)
}

override func placeholderRectForBounds(bounds: CGRect) -> CGRect {
    return CGRectOffset(bounds, textOffset.x + leftViewOffset, textOffset.y)
}

override func editingRectForBounds(bounds: CGRect) -> CGRect {
    return CGRectOffset(bounds, textOffset.x + leftViewOffset, textOffset.y)
}
Run Code Online (Sandbox Code Playgroud)

leftViewOffset是文本字段leftView的宽度(如果存在).textOffset是一个CGPoint,它定义要应用于文本rects的自定义x和y偏移量.

我的问题出现在我的签到视图中.我有2个CustomOffsetTextField实例 - 一个用于用户的电子邮件,另一个用于他们的密码.

在第一次加载视图控制器时,如果我在一个字段中输入文本然后点击另一个字段,那么该文本将在跳回到textRectForBounds定义的位置之前短暂地跳回其文本字段中的位置0,0.一些基本的打印调试验证这些函数总是返回我期望它们的值.

在这个初始打嗝之后,文本字段的行为就像我期望的那样.此问题仅在视图控制器加载后在每个文本字段中出现一次.在那之后,我可以在我想要的地方之间来回切换,而不会再次发生.

有没有人在iOS 9中看到与UITextField类似的问题?如果是这样,你能找到修复方法吗?

Jes*_*sse 6

在iOS 9中,UIKeyboardWillShowNotification只要您点击文本字段,就会发送额外的通知.如果您[self.view layoutIfNeeded]在通知回调中有呼叫,则会导致跳转.

// Animate
[UIView beginAnimations:@"keyboardDidShowAnimations" context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
[self.view layoutIfNeeded];
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

它与此有关:https: //forums.developer.apple.com/message/53905#53905

如果您在没有软件键盘的情况下在模拟器中进行测试,那么您将获得额外的功能UIKeyboardWillHideNotification,如果您在该通知回调中也有一个layoutIfNeeded调用,则可能会导致同样的问题.

我通过在回调的顶部放置检查来解决这个问题,以确保我真的需要动画/更新约束.

- (void)keyboardWillShow:(NSNotification *)note {
    BOOL shouldAnimate = self.someConstraint.constant != kMinimumSize;
    if (shouldAnimate) {
...

- (void)keyboardWillHide:(NSNotification *)note {
    BOOL shouldAnimate = self.someConstraint.constant == kMinimumSize;
    if (shouldAnimate) {
...
Run Code Online (Sandbox Code Playgroud)

更新: 对于多次调用通知方法的第三方键盘,这不一定正常.请参阅/sf/answers/1820322381/.