Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict my app to landscape mode?

I have my iPad application created using the SplitView template. I wonder what is the best way to restrict my application to landscape mode?

I have tried overriding shouldAutorotateToInterfaceOrientation: method in DetailViewController.m

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

but 4.2 GM is still buggy and it fails to show the controller view. What other choices do I have?

Thanks in advance.

UPDATE1

  • I have already filed a bug report: Bug ID #8620135

  • My app is almost finished and I have to find a work-arround since I don't think they are going to solve this before 4.2 officially comes out (GM is already out!)

    In order to recreate the bug, just use SplitView template and override above method in any of the UIViewControllers (RootViewController or DetailViewControllers)

UPDATE2

I have found a work-around. (See UPDATE3 for the complete work-around)

Set UISupportedInterfaceOrientations only to support Landscape , this will force the app to start in landscape mode allowing DetailViewController to start correctly(hence shown correctly)

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>

But if you rotate the device, it turns Portrait mode!!!, so is still necessary to override shouldAutorotateToIntercafeOrientation: as above

Discussion:

If this wouldn't be a bug I would expect a warning or execution error, exception or something when starting the app in a orientation that is not supported by the view controller. Besides, why only DetailViewController does not show? If this would be specification, then RootViewController should also fail to load then. Don't you think? thanks for you help... ;)

UPDATE3

After further tests I have realized that above work-around does not work in some cases. For example when starting the app when the device is in landscape won't work!. The real problem seems to be that in iOS4.2GM UISplitViewController needs all its controllers have all rotations to be available at its load time. So is necessary to trick him so it loads in Landscape mode and then not allow him to rotate its view controllers.

So here is the new work-around for this annoying iBug.

Step1: Set Info.plist like so:

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>

Step2 Set a new flag in DetailViewController.m or .h (from SplitView Template)

BOOL lockRotation = NO; //WORK-ARROUND: Bug ID# 8620135.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    //WORK-ARROUND: Bug ID# 8620135.
    if (lockRotation) {
        return UIInterfaceOrientationIsLandscape(interfaceOrientation);
    }else{
        return YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //set NO here since this is called before shouldAutorotateToInterfaceOrientation method is called
    lockRotation = NO; //WORK-ARROUND: Bug ID# 8620135.
}
- (void)viewDidAppear:(BOOL)animated {
    //set YES as soon as possible, but after shouldAutorotateToInterfaceOrientation method was called
    lockRotation = YES; //WORK-ARROUND: Bug ID# 8620135.
    [super viewDidAppear:animated];
}

IMPORTANT NOTE: Please note that this bug only appears when the UISplitViewController is loaded and not everytime the its view appears. Hence, to see this bug make sure the app was terminated before.

like image 617
nacho4d Avatar asked Nov 02 '10 14:11

nacho4d


People also ask

How do I force an app to landscape?

When on the main screen, under the orientation section, you will see a number of options like 'Auto-rotate OFF', 'Auto-rotate ON', 'Forced Portrait' and 'Forced Landscape'.

How do I restrict orientation on Android?

Add android:screenOrientation="portrait" to the activity you want to disable landscape mode in.


2 Answers

I asked a question with a bounty of 500 that seems to be the same thing you're facing.

From my limited experience it is much easier to make a landscape-only iPhone app than a landscape-only iPad app. I'm not sure why there is any difference, but the steps Apple says to take to make it landscape-only do not work on their own.

like image 98
Jonathan. Avatar answered Sep 30 '22 09:09

Jonathan.


Try this (it works):

-(BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)toInterfaceOrientation {
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
        return YES;
    }
    else 
    {
        return NO;
    }
}
like image 43
Sid Avatar answered Sep 30 '22 09:09

Sid