ConvertRect:toView 不返回预期的窗口坐标
目前,我正在尝试获取 UIWebView 的坐标。该 webView 是 viewController 视图的子视图。 viewController 包含在 UINavigationController 内。状态栏和导航栏都在屏幕上。
方向是纵向。该视图已以模态方式呈现。
状态栏的高度为 20,导航栏的高度为 44,我预计 webView 的框架(在窗口坐标中)的原点为 (0, 64),宽度为 (320, 416)。
但是,当我运行这条线时,
CGRect frame = [webView.superview convertRect:webView.frame toView:nil];
我得到的原点为 (0, 0),宽度为 (320, 416)。
我需要做什么才能获得我期望的正确来源?我是否错误地使用了这个方法?或者由于某种原因我的期望是错误的?
Currently, I am trying to get the coordinates of a UIWebView
. This webView is the subview a viewController's view. The viewController is contained inside of a UINavigationController
. The status bar and the navigation bar are both on screen.
The orientation is portrait. The view has been presented modally.
With the height of status bar at 20 and the height of navigation bar at 44, I would expect the frame of the webView (in window coordinates) would have an origin at (0, 64) and a width of (320, 416).
However, when I run this line
CGRect frame = [webView.superview convertRect:webView.frame toView:nil];
I get an origin of (0, 0) and a width of (320, 416).
Any ideas what I need to do to get the correct origin I am expecting? Am I using this method incorrectly? Or are my expectations wrong for some reason?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这完好无损是很奇怪的。我使用一个由窗口、导航控制器、视图控制器和 Web 视图组成的非常基本的应用程序进行了测试。全部以编程方式创建。代码如下,最后我绘制了所有视图框架,而最后我绘制了转换后的矩形:
结果如下:
这是您所期望的。您可能在视图在层次结构中的放置方式上犯了一些错误。例如,您是否使用 Interface Builder,在这种情况下您可以检查是否没有在视图位置放置错误的坐标?
This is quite strange intact. I did a test with a very basic application made of a window, a navigation controller, a view controller and a web view. All created programmatically. The code is the following and at the end I plot all view frames while at the end I plot the converted rect:
The results are the following:
which are the one you were expecting. Probably you're doing some mistake in the way the views are placed in the hierarchy. Are you for example using Interface Builder and in such case can you check that you didn't put the wrong coordinates in the view positions?