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.
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!
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:^{
}];
}];
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… ;(
来源:https://stackoverflow.com/questions/18980853/force-landscape-orientation-in-one-view-controller