iPad UITextField - 使用 contentVerticalAlignment = UIControlContentVerticalAlignmentCenter 调整框架大小时光标未居中
我正在开发一款通用的 iPhone/iPad 应用程序。我根据用户输入调整一些 UITextField 的大小。我还有一些代码来移动 UITextFields,以便它们的位置在调整大小时有意义。这段代码并不太短,但基本上可以归结为修改 UITextFields 的框架。
我的 UITextFields 的内容都垂直居中
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
通过设置文本中心, 。但是,当我在 iPad 上调整文本字段的大小时,光标有时会移到框的顶部。这对用户来说看起来很奇怪,因为文本位于中心。
这种情况总是发生在 iPad 上,而不会发生在 iPhone 上。更糟糕的是,我无法隔离代码的哪个特定部分导致它发生——除非我停止调整大小,问题就会消失。
还有其他人遇到过这种情况吗?您能够隔离问题的根源吗?
谢谢。
I am working on a universal iPhone/iPad app. I resize some of my UITextFields as the user types. I also have some code to move the UITextFields around so that their locations make sense as they are resized. This code is not too short, but basically it comes down to modifying the frames of the UITextFields.
My UITextFields all have their content vertically centered by setting
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
The text centers just fine. But when I resize the textfield on the iPad, the cursor sometimes goes to the top of the box. This looks strange to the user as the text is in the center.
This always happens on the iPad, never on the iPhone. Making matters worse, I can't isolate which particular part of the code causes it to happen -- except that if I stop resizing things, the problem goes away.
Has anyone else ever encountered this? Were you able to isolate the source of the problem?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我弄清楚是什么触发了这个错误。当通过layoutSubviews 和UIControlEventEditingChanged 触发的方法调整textField 的大小时,就会发生这种情况。
I figured out what triggers the bug. It happens when textField is resized both by layoutSubviews and by a method triggered by UIControlEventEditingChanged.
为了更好地控制文本字段文本对齐方式,您可以使用 UITextField 的子类,在其中重写以下方法:
For better control of text field text alignment you can use subclass of UITextField where you override following methods: