UITextField 文本在编辑前不可见 [英] UITextField Text Invisible Until Editing

查看:25
本文介绍了UITextField 文本在编辑前不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了 UITextField 的一个子类,它具有如下填充:

发生这种情况时,我还会在控制台中看到以下消息:

-[显示]:忽略伪造层尺寸(179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000,20.000000),contentsScale 3.000000,后备存储大小(INF,60.000000)

我想也许 bounds 值不正常,这就是导致视觉错误和错误的原因,但是当我记录边界时,我得到一堆似乎是有效,例如:{{0, 0}, {345, 40}}

注释掉上面这两种方法,基本上是一个普通的 UITextField,似乎解决了这个问题.我尝试在每个方法中添加对 super 的调用,以防万一,但无济于事.有什么想法吗?

Update - 将调用 becomeFirstResponder 移动到 viewDidAppear 似乎也解决了这个问题,但那很漂亮在基本上是一个向导的过程中,很多东西会扼杀流程,因此前一个屏幕也有屏幕上的键盘,所以我想避免键盘消失并重新出现在每个屏幕上.

解决方案

感谢@matt,我发现问题是因为我在 viewWillAppear 的文本字段上调用了 becomeFirstResponder.尽管这对我使用普通文本字段和文本视图多年来一直没有问题,但似乎这最终是出现问题的一个实例.

根据becomeFirstResponder 的 UIResponder 文档:

<块引用>

你可以调用这个方法来创建一个响应者对象,比如一个视图急救人员.但是,您应该只在该视图上调用它,如果它是视图层次结构的一部分.如果视图的 window 属性包含一个UIWindow 对象,它已经安装在一个视图层次结构中;如果它返回 nil,视图与任何层次结构分离.

这是通过在调试器中打印 [[self.textfield superview] window] 来验证的,并且在 viewWillAppear 中,窗口是 nil,而它不在 nil代码>viewDidAppear.每天学习新东西.

I've implemented a subclass of UITextField that has padding as follows:

methods taken from here

- (CGRect)textRectForBounds:(CGRect)bounds {
    return CGRectInset(bounds, 10, 10);
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    return CGRectInset(bounds, 10, 10);
}

With a combination of the above, setting the textField as the firstResponder in viewWillAppear:, and having an initial starting value in the field, I've run into an issue where the text is invisible until the field is edited, and then is fine after that:

When this occurs, I also see the following message in the console:

-[<CALayer: 0x15f000da0> display]: Ignoring bogus layer size (179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000, 20.000000), contentsScale 3.000000, backing store size (inf, 60.000000)

I thought perhaps the bounds value was out of whack, and that was what was causing both the visual bug and error, but when I log the bounds I get a bunch of values that appear to be valid, such as: {{0, 0}, {345, 40}}

Commenting out those two methods above, to basically be a normal UITextField, seems to fix the issue. I've tried adding calls to super in each of those methods, just in case that were to help, to no avail. Any ideas?

Update - Moving the call to becomeFirstResponder to viewDidAppear seems to also fix the issue, but that pretty much kills the flow in what is basically a wizard, and thus the previous screen also has the keyboard on screen, so I'd like to avoid the keyboard dismissing and reappearing on every screen.

解决方案

Thanks to @matt, I discovered that the issue is caused because I'm calling becomeFirstResponder on the textfield in viewWillAppear. Although this has worked for me with normal textfields and textviews for years without issue, it appears this is finally an instance where an issue arose.

According to the 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.

This was validated by printing [[self.textfield superview] window] in the debugger, and in viewWillAppear the window is nil, whereas it isn't in viewDidAppear. Learn something new every day.

这篇关于UITextField 文本在编辑前不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆