【发布时间】:2014-10-13 09:43:22
【问题描述】:
我看到UITextField 出现了一些非常奇怪的行为。我实现了一个自定义键盘,它在 iOS 7+ 上运行良好。但是,在 iOS 6 上,当调用以下行时(在执行“退格”之后),文本字段的光标消失,如果不辞职并再次成为第一响应者,则无法编辑,并且占位符和实际文本重叠。
这是我的“退格”功能的相关代码:
//Get the position of the cursor
UITextPosition *selStartPos = self.textBeingEdited.selectedTextRange.start;
int start = (int)[self.textBeingEdited offsetFromPosition:self.textBeingEdited.beginningOfDocument toPosition:selStartPos];
//Make sure the cursor isn't at the front of the document
if (start > 0) {
//Remove the character before the cursor
self.textBeingEdited.text = [NSString stringWithFormat:@"%@%@", [self.textBeingEdited.text substringToIndex:start - 1], [self.textBeingEdited.text substringFromIndex:start]];
//Move the cursor back 1 (by default it'll go to the end of the string for some reason)
[self.textBeingEdited setSelectedTextRange:[self.textBeingEdited textRangeFromPosition:[self.textBeingEdited positionFromPosition:selStartPos offset:-1] toPosition:[self.textBeingEdited positionFromPosition:selStartPos offset:-1]]];
//^This line is causing the issue
}
这就是我在 iOS 6 上看到的:
有人对此有任何见解吗?谢谢!
编辑
对于每个设置为 offset 的非零值似乎都会发生这种情况。
【问题讨论】:
标签: ios objective-c ios6 uitextfield uitextposition