Developer Forums relocated!

Need help with Apple Developer tools and technologies? Want to share information with other developers and Apple engineers? Visit Developer Forums at Apple.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

iPhone Subviews shift down by 20 pixels

Hi guys,

I'm working on an app where I have a main windows which contains a view. Based on a window switching tutorial, I then use [self.view insertSubview:aView atIndex:0] to add a subview to the main windows view. (later on I use animation and a second view, hence the need for a view and subview).

Anyway, the issue I have is that every time I add the subview, it is shifted 20 pixels down the phones screen. ie. there is a 20 pixel gap underneath the status bar and above the subview. It appears that in adding the subview to the main window's view, the sdk is deciding to allow extra space for the status bar. Even though this is already handled.

I have another test project where I do the same thing and it works just fine. The subview is not shifted. I've gone through all the code and IB settings and cannot figure out why the second project is shifting the subview down.

I have not been able to find a good explanation of what triggers this behaviour. Can anyone clarify what is going on and how to stop it happening ?


Thanks
Derek

MacBook Pro 17, Mac OS X (10.6.2)

Posted on Jan 5, 2010 5:09 AM

Reply
7 replies

Jan 5, 2010 4:08 PM in response to drekka

drekka wrote:
Hmm, would I be correct in assuming that views automatically assume they are being added to windows and therefore shift themselves down 20 pixels?

Close... But UIView isn't the culprit. Generally when a content view (view that wants to be full-screen) is shifted up or down 20 pixels from where it belongs, it's because the view controller isn't cooperating with IB (or in some cases because there isn't any view controller). Both of these actors have a mind of their own, and both think they're smarter than the programmer. Typically IB has adjusted the y-origin +20 when a simulated status bar has been turned on, then at run time, the view controller sees the real status bar and adds another +20, producing a 20-pixel gap between the status bar and the top of the content view.

While I can't tell you exactly what you did or didn't do to deserve this error, I can say that it seems to be sensitive to the sequence of actions you take in IB when adding or attempting to re-size the content view, and/or turning the simulated status bar on or off. To complicate matters, whenever IB thinks you don't know what you're doing, it can decide to re-size or move your content view when you aren't looking. This means that you should revisit the frame coordinates in the size inspector after the final save of an xib file.

You have several tools at your disposal to stay one step ahead of the software. Firstly I suggest you choose contrasting background colors for your window and content view(s) so you'll always know which is which and can easily see any offset. Secondly, add some logging someplace where it will run after the content view is loaded; e.g.:

- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"self.view=%@", self.view);
// uncomment the following only if desperate
// self.view.frame = CGRectMake(0, 20, 320, 460);
// NSLog(@"self.view=%@", self.view);
}

I also recommend setting up the xib window in IB so you always know which object you're inspecting when checking size and attributes. To do this, locate the View Mode switch in the upper-left corner of the xib window (this is the window with the icons, if it's not visible select Window->Document in the IB menu). Set View Mode to the Center position. The xib window should display a two column table with a tree view to the left. You can now see the view hierarchy and select the exact object you want to inspect (you can also modify the hierarchy by dragging in the tree).

Apropos the example code comments, in an emergency you can always force the frame back where it belongs. But I would encourage you to go back into IB and try to fix it there. For example, select the main window, open the Attributes Inspector and turn the simulated status bar on and off with the picker under "Simulated Interface Elements". Look for any change in the y-origin of the content view in the Size Inspector when you add or remove the simulated bar. Can you change the y-origin in the Size Inspector, or is it locked down (grayed out)? If the frame coords are locked, delete the view and see if the frame gets set better when you start with a new view. If you're only dealing with a status bar (no nav, tab, or tool bars), you probably want the content view frame to be 0, 20, 320, 460.

As you can see btw, all of the above assumes the content view will only be in portrait orientation. If landscape is involved, I would still recommend starting with a portrait-only testbed. Once you get the hang of things in portrait, you can move on to landscape.

Hope that helps!
- Ray

Jan 6, 2010 4:51 AM in response to drekka

What fun. In the xib window I had the view under it's controller. This meant that in the app delegate's applicationDidFinishLaunching I had to use addSubview to add the view to the main window. This was shifting the view down the window an extra 20 pixels. The funny thing was that the colour of the gap was dark grey, not the back of the window's background.

I then commented out the addSubview line in the app delegate and moved the view from being under the controller to being directly on the UIWindow. Now it appears correctly.

I checked all the size and position settings for everything and everything looked fine in IB. So the only conclusion that appears to make any sense is that something (the view's controller?) was shifting the view down when I added it using addSubview.

The view controller in IB has no background colour setting but does get drawn as dark grey so I'm wondering if that was being picked up when the shift was done.

Dunno. This doesn't make much sense at this time 😟

iPhone Subviews shift down by 20 pixels

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.