获取 Objective C iPad 应用程序中任何控件的绝对位置
我想知道是否有办法可以获取 ipad/iphone 应用程序中控件的绝对位置。例如,我有一个 TextField,它位于视图子视图的子视图中。我想知道与顶部父视图相关的 X 和 Y 值(例如,当前文本字段的 x 和 Y 返回 10 和 10,因为这是它在其自己的视图中的框架位置,但我想知道这与它的父级应该类似于 X = 10 和 Y = 220)。我需要为此制定一个通用方法。希望这是有道理的。
有什么想法吗?
Im wondering if there is anyway that you can get the absolute location of a control in a ipad/iphone application. e.g. I have a TextField which is within a child of a child of a view. I want to know the X and Y values in relation to the top Parent View (e.g. currently the x and Y of the textfield return 10 and 10 because that is it's frame location within its own view, but I want to know this in relation to its parent which should be something like X = 10 and Y = 220). I need to make a generic method for this somehow. Hope this make sense.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找
-[UIView ConvertPoint:toView:]
。例如,要根据窗口的基坐标获取视图
view
的原点,您可以这样写:如果您想将该点转换为同一窗口内其他视图的坐标系,您可以使用该视图作为
toView:
参数而不是nil
:因为不同的坐标系可以有不同的比例,您可能会发现
-convertRect:toView:< /code> 更有用,具体取决于什么你打算处理坐标。还有类似的
-fromView:
版本的点和矩形转换方法。You are looking for
-[UIView convertPoint:toView:]
.For example, to get the origin of a view
view
in terms of its window's base coordinates, you would write:If you instead want to convert the point into the coordinate system of some other view within the same window, you can use that view as the
toView:
argument instead ofnil
:Because different coordinate systems can have different scales, you might find
-convertRect:toView:
to be more useful, depending on what you're planning to do with the coordinates. There are also analogous-fromView:
versions of both the point and rect conversion methods.