mdonati
mdonati

Reputation: 1099

Force landscape orientation in one view controller

In iOS 5 and 6 I was doing this in the viewWillAppear method in my view controller:

UIViewController *c = [[UIViewController alloc] init];
//To avoid the warning complaining about the view not being part of the window hierarchy
[[[TWNavigationManager shared] window] addSubview:c.view];
[self presentModalViewController:c animated:NO];
[self dismissModalViewControllerAnimated:NO];
[c.view removeFromSuperview];

I also added this method in the app delegate

- (NSUInteger)application:(UIApplication *)application      supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return [[TWNavigationManager shared] supportedInterfaceOrientationsForTopViewController];
}

Which basically forwards that call to the top view controller.

That caused auto-rotation methods to be called for my view controller and then I was able to force landscape orientation for just that view controller. Now in iOS 7 that code doesn't work anymore. A white view appears full-screen.

What would be the proper approach in iOS7?

Thanks in advance.

Upvotes: 2

Views: 2188

Answers (3)

Muscovado
Muscovado

Reputation: 53

To prevent the little "flashing" from mdonia solution, I added a dispatch_after and was able to dismiss the dummy modal viewController with animation:NO

UIViewController *dummyModalVC = [UIViewController new];
[dummyModalVC setModalPresentationStyle:UIModalPresentationFullScreen];
[dummyModalVC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[dummyModalVC.view setBackgroundColor:[UIColor purpleColor]];

[self presentViewController:dummyModalVC animated:NO completion:^{
    double delayInSeconds = 0.001f;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [dummyModalVC dismissViewControllerAnimated:NO completion:^{}];
    });
}];

Looks of course still like an ugly workaround, but I didn't found a better solution in the given time… ;(

Upvotes: 0

mdonati
mdonati

Reputation: 1099

My solution involves what Andrey Finayev suggested, but also I had to set another transition style otherwise I was getting blank screen after the dismiss animation finished.

UIViewController *mVC = [[UIViewController alloc] init];
mVC.modalPresentationStyle = UIModalPresentationFullScreen;
mVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.navigationController presentViewController:mVC animated:NO completion:^{
    [self.navigationController dismissViewControllerAnimated:YES completion:^{

    }];
}];

Upvotes: 0

Andrey Finayev
Andrey Finayev

Reputation: 111

Had the same problem and managed to fix it by dismissing the presented modal view animated:YES.

[self dismissViewControllerAnimated:YES completion:nil];

Hope that helps!

Upvotes: 5

Related Questions