Force landscape orientation in one view controller

百般思念 提交于 2019-12-07 23:59:57

问题


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.


回答1:


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!




回答2:


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:^{

    }];
}];



回答3:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!