【问题标题】:Force landscape orientation in one view controller在一个视图控制器中强制横向
【发布时间】:2013-09-24 11:50:57
【问题描述】:

在 iOS 5 和 6 中,我在视图控制器的 viewWillAppear 方法中执行此操作:

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];

我还在应用委托中添加了这个方法

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

这基本上将该调用转发到顶视图控制器。

这导致为我的视图控制器调用自动旋转方法,然后我能够为该视图控制器强制横向方向。 现在在 iOS 7 中,该代码不再起作用。全屏显示白色视图。

iOS7 中的正确方法是什么?

提前致谢。

【问题讨论】:

  • 你从哪里展示这个 lanscape-only-viewController ?它是导航堆栈中的“推送”吗?还是模态呈现的视图控制器? (在这种情况下更容易)
  • 是的,这是在导航堆栈中的推送。我的根视图控制器是一个 UITabBarViewController,每个项目都有一个 UINavigationController。

标签: ios7 autorotate


【解决方案1】:

遇到了同样的问题并设法通过关闭呈现的模态视图动画来解决它:是的。

[self dismissViewControllerAnimated:YES completion:nil];

希望有帮助!

【讨论】:

    【解决方案2】:

    我的解决方案涉及 Andrey Finayev 的建议,但我还必须设置另一种过渡样式,否则在关闭动画完成后我会出现空白屏幕。

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

    【讨论】:

      【解决方案3】:

      为了防止 mdonia 解决方案的小“闪烁”,我添加了一个 dispatch_after 并且能够通过动画关闭虚拟模式视图控制器: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:^{}];
          });
      }];
      

      看起来当然仍然是一个丑陋的解决方法,但我在给定的时间内没有找到更好的解决方案……;(

      【讨论】:

        猜你喜欢
        • 2013-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-15
        • 2012-12-09
        • 2011-09-27
        相关资源
        最近更新 更多