iPhone UI布局调试

发布于 2024-08-27 11:37:24 字数 720 浏览 6 评论 0原文

我在 iPhone UI 开发中遇到了这个长期问题,视图有时似乎出现在屏幕上的位置与其框架属性报告的位置不同。以下是我尝试调试问题的方法:

UIView *currentView = self.view;
while (currentView!=nil)
{
   NSLog(@"frame: %f,%f,%f,%f", currentView.frame.origin.x, 
                                currentView.frame.origin.y,
                                currentView.frame.size.width,
                                currentView.frame.size.height);
   currentView = currentView.superview;
}

期望这应该显示从给定视图到应用程序的层次结构中每个元素的坐标和大小根 UIWindow 元素,每个元素相对于其父元素的坐标。然而,情况似乎并非如此。在我目前的情况下,我正在尝试调试一个 UI,每次旋转设备时,整个 UI 都会向上或向下移动 20 像素,但上面的代码块每次都会报告完全相同的数字。我尝试在延迟一秒后调用上面的代码,但每次得到的数字仍然相同。

有谁知道检查 UI 元素的屏幕坐标的更好方法吗?如果我能发现错误,那么当问题出现时我就能弥补。

I have this chronic issue with iPhone UI development where views sometimes seem to appear on the screen in a location different than what is reported by their frame property. Here is what I am doing to try to debug the issue:

UIView *currentView = self.view;
while (currentView!=nil)
{
   NSLog(@"frame: %f,%f,%f,%f", currentView.frame.origin.x, 
                                currentView.frame.origin.y,
                                currentView.frame.size.width,
                                currentView.frame.size.height);
   currentView = currentView.superview;
}

I expect this should show me the coordinates and size of each element up the hierarchy from the given view to the app's root UIWindow element, with the coordinates for each element relative to its parent. However, that does not seem to be the case. In my current situation, I have a UI I'm trying to debug where every other time I rotate the device, the whole UI shifts up or down 20 pixels, yet the code block above reports exactly the same numbers every time. I tried calling the above code after as much as a second delay, but that the numbers still come out the same each time.

Does anyone know a better way to inspect the screen coordinates of UI elements? If I can detect when one is wrong, I can compensate for the problem when it appears.

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

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

发布评论

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

评论(1

埋葬我深情 2024-09-03 11:37:24

您可以尝试一些事情:

  • 您可以使用 UIViewrecursiveDescription 方法(未记录,但效果很好),而不是自己动手。 (参考
  • 将您的代码更改为 NSLog 点就其他视图而言,使用名为“convert{Point,Rect}:{to,from}View:”的四种方法。例如,在应用程序的窗口上调用 convertRect:fromView:,从子视图中传递一堆帧。

我之前也遇到过一个奇怪的 20 像素错误。我从未 100% 解决过这个问题,但我有两个想法可能会有所帮助:

  • 仔细检查您的应用首次启动时是否在 window 对象上调用 makeKeyAndVisible。如果你的窗口没有设置为“关键”,那么一些微妙的奇怪的事情可能会出错。
  • 尝试在调整框架大小并重置框架时找到代码中的点。在操作系统为您搞乱之后,您可能必须使用performSelector:withObject:afterDelay: 方法来重置它。请查看 NSRunLoop 上的这篇文章,了解有关该方法为何有用的更多详细信息。

Some things you can try out:

  • Instead of rolling your own, you can use the recursiveDescription method of UIView (undocumented, but works great). (Reference)
  • Alter your code to NSLog the points in terms of other views using the four methods called convert{Point,Rect}:{to,from}View:. E.g., call convertRect:fromView: on your app's window, handing in a bunch of frames from subviews.

I also previously had a weird off-by-20-pixels bug. I never 100% solved it, but I had two ideas that might help:

  • Double-check that you're calleding makeKeyAndVisible on your window object when your app first starts. If your window is not set as "key" then some subtle weird things can do wrong.
  • Try to find the point in your code when the frame is resized and reset it. You might have to use the performSelector:withObject:afterDelay: method to reset it after the OS messes it up for you. Check out this post on NSRunLoop for more details on why that method is useful.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文