PonySlays
PonySlays

Reputation: 37

Force Landscape view in IOS6

I know that there are lots of threads about how to force an orientation in IOS6 but none of them seems to work for me so now I need some help figuring this out.

I have a navigation based app that has many view controllers. All of them are in portrait view exept one that has to load in landscape mode (without having the user turning the phone first).

In the implementation of my navigation controller, I have added shouldAutorotate, supportedInterfaceOrientations and preferredInterfaceOriantationForPresentation.

@implementation UINavigationController (Rotation_IOS6)

- (BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

@end

So it returns the values that have then defined in each view controller.

In the app delegate I have the supportedInterfaceOrientationsForWindow.

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{

//Default orientations value
NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;

//Get orientation from the last view controller
if(self.window.rootViewController){
    UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
    orientations = [presentedViewController supportedInterfaceOrientations];
}

return orientations;
}

In each view controller I then have my settings for that view, for example:

// Only allow portrait view
-(NSInteger)supportedInterfaceOrientations{    
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate {
    return YES;
}

And I push the next view controller like this:

NextViewController *nxtController = [[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil];
[self.navigationController pushViewController:nxtController animated:YES];

But when I push the landscape view controller, while holding the phone in portrait orientation, It also loads in portrait mode. If I then tilt the phone it triggers the autorotate function and rotates the view into landscape mode, and then it is locked in that mode. However I need to lock it in landscape mode without using the phones orientation to trigger it to check autorotate. Any ideas?

Upvotes: 2

Views: 725

Answers (2)

Prajwal Udupa
Prajwal Udupa

Reputation: 860

go to project settings and remove the selection on the modes you do not want.

Upvotes: 1

perrohunter
perrohunter

Reputation: 3526

You could try to force the viewController to show in landscape by using the shouldaAutorotateToInterfaceOrientation that always return landscape, such as:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}

Upvotes: 1

Related Questions