0
votes

In my app i am supporting landscape and portrait orientation for a single ViewController. I can use Autoresize to support both the landscape and portrait. But i need to make custom landscape which differs from portrait. I am very new to iOS. Searched a lot in google and SO but couldn't find the solution.

I am using Xcode 4.5 and storyboard to make Views.

How to support custom landscape and portrait view?

Any help will be much appreciated.

2

2 Answers

2
votes

Try this out in your .m file:

- (void)updateLayoutForNewOrientation:(UIInterfaceOrientation)orientation
{
    if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        // Portrait

        [object setFrame:CGRectMake(...)];

        // Do the same for the rest of your objects
    }

    else
    {
        // Landscape

        [object setFrame:CGRectMake(...)];

        // Do the same for the rest of your objects
    }
}

In the function, you have defined the position of each object in your view, for both Portrait and Landscape.

Then you call that function in viewWillAppear to initially make it work; the view determines which orientation to use at start:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self updateLayoutForNewOrientation:self.interfaceOrientation];
}

Also, for when you rotate:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration 
{    
     [self updateLayoutForNewOrientation:self.interfaceOrientation];
}

This is the approach I take if I need a more customized look in regard to orientation. Hope that'll work for you.

EDIT:

If you use two UIViews, one for portrait and another for landscape, in one UIViewController, you would change that first part of the code to look like this:

- (void)updateLayoutForNewOrientation:(UIInterfaceOrientation)orientation
{
    if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        // Portrait

        portraitView.hidden = NO;
        landscapeView.hidden = YES;
    }

    else
    {
        // Landscape

        portraitView.hidden = YES;
        landscapeView.hidden = NO;
    }
}

There are pros and cons between this edited sample and the original. In the original, you have to code for every object, in this edited sample this code is all you need, however, you need to essentially allocate objects twice, one for a portrait view and another for a landscape view.

1
votes

YOu can still use Sean's approach but since you have 2 different views you probably have 2 different Xib files so instead of the CGRect section you can do something like [[NSBundle mainBundle] loadNibNamed:"nibname for orientation" owner:self options:nil]; [self viewDidLoad];. I don't exactly how this would work with storyboards since I haven't used it yet, but I did this in an application that needed a different layout in the orientations so I created 2 Xib files and hooked them both up to the ViewController so on rotation the appropriate Xib file is loaded.