Get UITextField Y coordinate on UIView level?

烈酒焚心 提交于 2019-12-12 10:54:59

问题


Currently I have a UITextField in a custom cell and I want to get the Y coordinate of that UITextField on the level of the View Controller's view (UIView). Currently I am trying to do something like this:

if (thetextField.superview.superview.superview.frame.origin.y >= keyboardY) {

}

But it seems that the Y coordinate is always 0 when it is in fact not. Is there any way to achieve this?

I am doing this because I want to detect whether the UIKeyboard is blocking the UITextField on the UIView level so that I can re-arrange my cells accordingly.

Thanks!


回答1:


Check convertPoint:* methods in UIView documentation. These methods allow you to map point to or from other views coordinates.

Provided that self is a view controller, this line returns text field's origin in view controller's view coordinates:

[thetextField convertPoint:thetextField.frame.origin toView:self.view];

You can also convert rects in a similar fashion using convertRect:* methods.




回答2:


Swift 3 version:

thetextField.convert(thetextField.frame.origin, to:self.view)

It returns a CGPoint, so you can access "x" or "y" properties.



来源:https://stackoverflow.com/questions/9474160/get-uitextfield-y-coordinate-on-uiview-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!