iOS:convertRect:toView:无法按我的预期工作

发布于 2024-11-14 11:47:47 字数 639 浏览 2 评论 0 原文

我想要相对于主窗口的坐标,因此我使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尾戒 2024-11-21 11:47:47

如果您查看 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. Since self.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文