Question

I've got a massive problem and I really hope you can help me here... I'm very lost at the moment :(

I've got a project that runs with a MainWindow.xib. In my app delegate file I check the orientation of the device and load an appropriate NIB file that have different layouts (subviews) based on orientation. Here is the code to check the orientation:

-(void)checkTheOrientation
{
    if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) 
    {
        viewController = [[[MyViewController alloc] initWithNibName:@"MyWideViewController" bundle:nil] autorelease];
        NSLog(@"Landscape = MyWideViewController");
    } 
    else if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown)
    {   
        viewController = [[[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil] autorelease];
        NSLog(@"Portrait = MyViewController"); 
    }
}

This works as expected and in Landscape or Portrait I am loading the correct views. The portrait view loads perfectly but the Landscape view loads with a thick black edge to the left as if it's x & y positions are not set to 0 & 0 respectively.

Here is the portrait view: http://uploads.socialcode.biz/2f25352B0e3x3z2t2z21 Here is the landscape view with the bug: http://uploads.socialcode.biz/1G2k3T012d1z0Y1U2q1k

In the MyViewController.m file I have a rough fix to get the sizing done correctly to avoid this big black strip on the left. Here's the code for this:

- (void) performLayout {
    // Ensure the main view is properly placed
    NSInteger MaxSizeHeight, MaxSizeWidth;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    {
        MaxSizeHeight = 1024;
        MaxSizeWidth  = 768;
    }
    if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
        CGRect mainViewFrame = [[self view] frame];
        float width = mainViewFrame.size.width;
        mainViewFrame.size.width = mainViewFrame.size.height;
        mainViewFrame.size.height = width;
        mainViewFrame.origin.x = 0;
        mainViewFrame.origin.y = 0;
        [[self view] setFrame:mainViewFrame];
    } else {
        CGRect mainViewFrame = [[self view] frame];
        mainViewFrame.origin.x = MaxSizeWidth - mainViewFrame.size.width;
        mainViewFrame.origin.y = MaxSizeHeight - mainViewFrame.size.height;
        [[self view] setFrame:mainViewFrame];
    }

    // Ensure the content view is properly placed
    CGRect contentFrame = [mainContentView frame];
    if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) 
    {
        contentFrame.size.width = MaxSizeHeight;
        contentFrame.size.height = MaxSizeWidth;
    }
    contentFrame.origin.x = 0;
    contentFrame.origin.y = 44;
    [mainContentView setFrame:contentFrame];

    // Ensure the content subviews are properly placed
    contentFrame.origin.y = 0;
    for (UIView *contentView in [mainContentView subviews]) {
        [contentView setFrame:contentFrame];
    }
}

The problem with this method is that this is just a very bad hack and it's not actually solving my problem. When it loads up in Landscape it now resizes and positions the subview to 1024x768,0,0 but any additional subviews that I load via other NIBs have the same problem.

What I would really like to know is how on earth can I set the landscape main superview to be 1024x768 position 0 & 0 without having to try and hack this together and keep performing the performLayout selector? At the moment there is a lot of inconsistence with this as the hack doesn't actually set the superview sizing correctly but rather just the subviews I load on top of the superview.

I thought that maybe a simple fix like this might solve the superview issue but alas it doesn't: window.frame = CGRectMake(0, 0, 1024, 768);

I just want my main superview to be the placed and sized correctly at load and then the superviews just load in the right place. Please can you help me here???

Was it helpful?

Solution

First, UIView has a method named -layoutSubviews that you can override to position the subviews however you like. It'll be called when the view is first loaded, and again before drawing if you ever send it a -setNeedsLayout message.

Second, you should be able to properly set these positions in your .xib or storyboard file. Select your main view in its .xib file and check its position in the Size inspector. Also, ensure that the view is set to Landscape orientation in the Attributes inspector. If it's all correct, then there's something else going on. To simplify the problem, make a new project in Xcode with nothing but an empty view in landscape orientation. I just did one, and there's no position problem. That's what you want in your real app, so figure out what's happening in your real app to affect the view's position that's not happening in your new sample app.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top