0

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.

vinothp
  • 9,939
  • 19
  • 61
  • 103

2 Answers2

2

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.

Scott
  • 110
  • 1
  • 1
  • 9
  • Thanks for your answer. I am bit confused what goes in CGRectMake. Because currently i made two views using storyboard. One for Landscape and other one Portrait. How to link those two views in to your code – vinothp Oct 12 '12 at 15:55
  • CGRectMake is used to define where the interface objects are located, in addition to the size of these objects -- `CGRectMake(x-position, y-position, width, height)` -- I'll edit my answer to reflect what you would do if you were using two UIViews. – Scott Oct 12 '12 at 16:22
  • If my additional code is what you're looking for, you also might want to include `[landscapeView setFrame:CGRectMake(0, 0, width, height)]`, etc. in the updateLayout function, perhaps even the viewDidLoad, in case something gets accidentally relocated in Interface Builder. – Scott Oct 12 '12 at 17:25
  • Thanks for your detailed answer. I can't try it today because i didn't have Mac with me now. I will try this in monday and let you know +1 for your effort and support Cheers – vinothp Oct 12 '12 at 17:47
  • can you explain it more i don't know how i do that but i need use this orientation please help me .. –  Nov 20 '14 at 12:09
1

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.

Sal Aldana
  • 1,235
  • 4
  • 17
  • 39