View position not adjusting for status bar


#1

I’m seeing an issue with the HeavyRotation sample. It happens with the sample project from the website as well.

When the app first launches, the y position of the UI elements doesn’t take the status bar into account. For example, the slider y position is 39. After rotating to landscape and then back to portrait all of the UI elements are shifted down by 20 pixels (the height of the status bar) - the slider y position is now 59.

I fixed it by adding the following into the delegate’s applicationDidFinishLaunching:

[code]HeavyViewController *hvc = [[HeavyViewController alloc] init];

// Set the y origin of the view 20 pixels down to account for the height of the status bar
CGRect frame = [[hvc view] frame];
frame.origin.y += [[UIApplication sharedApplication] statusBarFrame].size.height;
[[hvc view]setFrame:frame];

[window addSubview:[hvc view]];[/code]

Why is this happening and is there a better way to solve this?

If I create the project starting with the “View-based Application” template I don’t have this issue and don’t need to manually adjust the origin.


#2

This is a bug with XIB files that Apple has sadly neglected for the last two years. My fix is the following:

- (void)viewDidLoad
{
    CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
    [[self view] setFrame];
}