我想要相对于主窗口的坐标,因此我使用 ConvertRect:toView: 并将 nil 作为第二个参数:
CGRect result = [self.view convertRect:addToList.frame toView:nil];
但结果,我总是得到
NSRect: {{5, 30}, {35, 35}}
,而这正是我用于生成 addToList- 的值视图:
addToList = [UIButton buttonWithType:UIButtonTypeCustom];
addToList.frame = CGRectMake(5.0f, 30.0f, 35.0f, 35.0f);
如果我将 addToList 按钮添加到位于屏幕右下角的视图中,我会期望一个 CGRect,例如 iPad 横向上的 950、700、35、35,因为这些是按钮相对于最顶层视图的坐标。
首先,我在 UIViewController 的 initWithFrame:-方法中进行了转换;但现在,我事后才这样做……我错过了什么?
谢谢!
编辑:“self.view”位于 UIScrollView 内重要吗?
I'd like to have the coordinates relative to the main window, therefore I'm using convertRect:toView: with nil as second parameter:
CGRect result = [self.view convertRect:addToList.frame toView:nil];
But as a result, I always get
NSRect: {{5, 30}, {35, 35}}
and that are exactly the values I use for generating the addToList-view:
addToList = [UIButton buttonWithType:UIButtonTypeCustom];
addToList.frame = CGRectMake(5.0f, 30.0f, 35.0f, 35.0f);
If I'm adding the addToList-button to a view which is placed in the bottom right corner of the screen, I would expect a CGRect like for example 950, 700, 35, 35 on an iPad landscape, because those are the coordinates of the button relative to the topmost view.
First, I did the conversion in the initWithFrame:-method of a UIViewController; but now, I'm doing it afterwards... what am I missing?
Thanks!
EDIT: is it important that "self.view" is inside a UIScrollView?
发布评论
评论(1)
如果您查看
convertRect:toView:
的文档,您将看到您传递的 rect 被定义为您正在其上调用该方法的视图的边界。由于 self.view 可能占据整个窗口,因此矩形不会相对于窗口发生变化。您应该向按钮的父级发送消息以获取按钮在窗口中的实际位置。If you look at
convertRect:toView:
's documentation, you will see that the rect that you pass is defined as within the bounds of the view on which you are calling the method. Sinceself.view
presumably takes the entire window, the rect doesn't change with respect to the window. You should message the parent of the button to get the actual location of the button w.r.t the window.