开发者

iPhone UI layout debugging

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, bu开发者_运维技巧t 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.


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜