iPhone UI layout debugging

Posted by Cruinh on Stack Overflow See other posts from Stack Overflow or by Cruinh
Published on 2010-03-30T18:17:34Z Indexed on 2010/03/30 19:03 UTC
Read the original article Hit count: 322

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.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about user-interface