iPhone UI布局调试
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试一些事情:
UIView
的recursiveDescription
方法(未记录,但效果很好),而不是自己动手。 (参考)NSLog
点就其他视图而言,使用名为“convert{Point,Rect}:{to,from}View:”的四种方法。例如,在应用程序的窗口上调用convertRect:fromView:
,从子视图中传递一堆帧。我之前也遇到过一个奇怪的 20 像素错误。我从未 100% 解决过这个问题,但我有两个想法可能会有所帮助:
window
对象上调用makeKeyAndVisible
。如果你的窗口没有设置为“关键”,那么一些微妙的奇怪的事情可能会出错。Some things you can try out:
recursiveDescription
method ofUIView
(undocumented, but works great). (Reference)NSLog
the points in terms of other views using the four methods called convert{Point,Rect}:{to,from}View:. E.g., callconvertRect: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:
makeKeyAndVisible
on yourwindow
object when your app first starts. If your window is not set as "key" then some subtle weird things can do wrong.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.