CJ_COIMBRA
CJ_COIMBRA

Reputation: 329

ViewController with locked orientation

I am having issues with the following situation:

ViewController A should be locked on landscape (left or right). The setup for VC A is the following:

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

-(BOOL)shouldAutorotate {
    return NO;
}

ViewController B should be locked on portrait. Its setup is the following:

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate {
    return NO;
}

There is no navigation controller, or tab bar. It is just a regular view controller and then on a button press I go from ViewController A to ViewController B with

[self performSegueWithIdentifier:@"ViewControllerB" sender:self];

Everything is fine so far. But when I dismiss ViewController B then ViewController A shows up on portrait. This is how I dismiss VC B:

[self dismissViewControllerAnimated:YES completion:nil];

Another problem is: when I am on ViewController B and it is OK on portrait, then I start to quickly rotate my device from portrait to landscape several times and it rotates to landscape at some point despite the orientation rules above. Is there something I am missing?

Upvotes: 0

Views: 81

Answers (1)

Soberman
Soberman

Reputation: 2576

In viewDidAppear: try using this method to rotate to your desired orientation, in case the orientation is wrong at the load of the controller:

NSNumber *value = @(UIInterfaceOrientationLandscapeRight); // Or whatever else orientation you need.
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];

Also redefine your shouldAutorotate method like this:

- (BOOL)shouldAutorotate {
    // Change the YES or NO order depending on your viewController.
    return [UIDevice currentDevice].orientation == UIDeviceOrientationPortrait ? YES : NO;
}

And implement this method as well:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait; // Or whatever orientation you need in this viewController.
}

Upvotes: 0

Related Questions