user574089
user574089

Reputation: 350

Auto orientation in iPhone

I have used UIDeviceOrientationDidChangeNotification to get notified of landscape and potrait mode. In my project I'm required to change the positions of UIControls placed on the UIView accordingly. Do I have to change the frame size manually for each UIControl, as this is hectic?

Upvotes: 0

Views: 192

Answers (2)

WolfLink
WolfLink

Reputation: 3317

You do need to use the shouldAutorotateToInterfaceOrientation... method but you need to do a little more before it will work properly. First of all, you will need two xib files, one for a landscape orientation and one for a portrait orientation. You will then need to use your function like this:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft){
    //prepare for a Landscape view
    }
else{
    //prepare for a portrait view
    }
    return YES;
}

As for preparing for landscape or portrait views, using autoresizing can work but you may need to have totally seperate xibs. I could not find a way to change the xib within the active view controller, so I would suggest making a seperate instance of your view controller. Example:

When you present your first instance of the viewController, present it with the portrait xib. You may also need a boolean to keep track of the current xib. Then have this code implemented:

/*Assuming you are using a viewcontroller called viewController, and you have a boolean called xibPortrait.
You have presented this view controller using the portrait xib.
You also have a landscape xib, Landscape.xib
*/

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ((interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft) && xibPortrait){
        xibPortrait = FALSE;
        viewController *Land = [[ViewController alloc] initWithNibName:@"Landscape" bundle:nil];
        [self presentModalViewController:Land animated:NO];
        [Land release];
    }
else if(!xibPortrait){
    [self.parentViewController dismissModalViewControllerAnimated:NO];
    xibPortrait = TRUE;
    }
return YES;
}


//This should work but if you can, you should try to use Autoresizing.

EDIT: I also found this which someone posted on one of my questions. It may help your app work more efficiently than using my method above: http://aseriesoftubes.com/articles/ipad-development-101-orientation/

Upvotes: 0

Nekto
Nekto

Reputation: 17877

In your view controller you should implement method:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

This method should return YES for supported orientations.

If you want your subviews to resize and reposition automatically then you should set appropriate property autoresizingMask of that views. For example:

blackView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight |
                                 UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | 
                                 UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;

Upvotes: 1

Related Questions