convertPoint:toView: in landscape mode giving wrong values

核能气质少年 提交于 2020-01-11 17:10:28

问题


I have a view which is created in landscape mode (long after rotation etc.).
In this view, I want to find a point relative to the main window.
The following code works in portrait mode, but in landscappe it still returns values as if it were in portrait.

CGPoint ptRelativeToWindow = [self convertPoint:self.bounds.origin toView:nil];

Solved

This solved the problem and gives the right coordinates:

[self convertPoint:self.bounds.origin toView:[UIApplication sharedApplication].keyWindow.rootViewController.view];

回答1:


You can't use main window to calculate relative coordinates. Main window receives rotation events and passes them onto controllers which means it doesn't change the size itself (always has the same portrait bounds). That's why the solution you found makes sense: you find the coordinates relative to the view of the root controller which does receive rotation events and changes its size accordingly



来源:https://stackoverflow.com/questions/6452716/convertpointtoview-in-landscape-mode-giving-wrong-values

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