Laurenz Glück
Laurenz Glück

Reputation: 1762

Different user interfaces for landscape and portrait orientations

I've long been a question to which I have not found a good answer.

Is it possible (it must! ;) to build different user interfaces for example the landscape and portrait or iPhone with 3,5" oder 4" display.

Like saying in code, THIS IS the landscape interface and THIS IS the portrait interface...

I hope there is a simple way to this. :)

With best regards from Munich, Germany

Laurenz

Upvotes: 0

Views: 225

Answers (1)

lobianco
lobianco

Reputation: 6276

This is how I do it. I made a function called configureViewForOrientation: that takes as a parameter the orientation that you want to set up the interface for.

-(void)configureViewForOrientation:(UIInterfaceOrientation)orientation
{
    if (UIInterfaceOrientationIsPortrait(orientation))
    {
        //set up portrait interface
    }

    else
    { 
        //set up landscape interface
    }
}

You can call this method from your viewDidLoad (or wherever you initially set up your interface) and feed it the current orientation using [UIApplication sharedApplication].statusBarOrientation:

-(void)viewDidLoad
{
    [super viewDidLoad];
    [self configureViewForOrientation:[UIApplication sharedApplication].statusBarOrientation];
}

You can also call it when your device is rotated:

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [self configureViewForOrientation:toInterfaceOrientation];
}

There are a couple caveats (such as whether you're checking device orientation immediately upon app launch) but this should work for the majority of the time.

Upvotes: 1

Related Questions