Search code examples
cocoaios7popoverscreen-rotation

iOS 7: Restrict device current orientation when modal view controller is presented


I have a UIViewController which contains a UICollectionView. On tapping any of the UICollectionViewCell I present a modal view controller.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
PopViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"DetailsView"];
vc.view.backgroundColor = [UIColor clearColor];
vc.transitioningDelegate = self;
vc.modalPresentationStyle = UIModalPresentationCustom;
[self presentViewController:vc animated:YES completion:nil];

The PopViewController shows up correctly. Now I want to restrict the device orientation when PopViewController is being presented. That is if PopViewController is presented in portrait mode then it should not change to landscape even if I switch to landscape mode (using Rotate Left or Right in simulator) until I dismiss the PopViewController.

I have used following code in PopViewController:

-(BOOL)shouldAutorotate {
    return NO;
}

What else (or instead) is needed to lock the pop up view to the current orientation?


Solution

  • in your modal controller try to add this, also (iOS > 6)

    -(NSUInteger)supportedInterfaceOrientations
    {
             return UIInterfaceOrientationMaskPortrait;
    }
    

    to support iOS 5 or below you must additional add:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
    {
    
       return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
    
    }