我已经实现了 UITextField 的子类,它具有如下填充:
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectInset(bounds, 10, 10);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return CGRectInset(bounds, 10, 10);
}
结合以上各项,设置 textField
作为 firstResponder
在viewWillAppear:
,并且在该字段中有一个初始起始值,我遇到了一个问题,即在编辑该字段之前文本是不可见的,然后就可以了:
发生这种情况时,我还在控制台中看到以下消息:
-[<CALayer: 0x15f000da0> display]: Ignoring bogus layer size (179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000, 20.000000), contentsScale 3.000000, backing store size (inf, 60.000000)
我想也许是 bounds
value 不正常,这就是导致视觉错误和错误的原因,但是当我记录边界时,我得到一堆似乎有效的值,例如:{{0, 0}, {345, 40}}
注释掉上面的这两种方法,基本上是一个普通的 UITextField,似乎可以解决这个问题。我试过添加对 super
的调用在每一种方法中,以防万一,但都无济于事。有什么想法吗?
更新 - 将调用转移到 becomeFirstResponder
至 viewDidAppear
似乎也解决了这个问题,但这几乎扼杀了基本上是一个向导的流程,因此前一个屏幕也有屏幕上的键盘,所以我想避免键盘消失并重新出现在每个屏幕上。
最佳答案
感谢@matt,我发现问题是因为我在 viewWillAppear
的文本字段上调用了 becomeFirstResponder
。虽然这对我来说多年来一直没有问题地使用普通文本字段和 TextView ,但看起来这最终是出现问题的一个实例。
根据UIResponder docs for becomeFirstResponder
:
You may call this method to make a responder object such as a view the first responder. However, you should only call it on that view if it is part of a view hierarchy. If the view’s window property holds a UIWindow object, it has been installed in a view hierarchy; if it returns nil, the view is detached from any hierarchy.
这是通过在调试器中打印 [[self.textfield superview] window] 来验证的,在 viewWillAppear
中窗口是 nil
,而在 viewDidAppear
。每天学习新东西。
关于ios - UITextField 文本在编辑前不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33127863/