【问题标题】:Allow One View to Support Multiple Orientations While Others Do Not iPhone允许一个视图支持多个方向,而其他视图不支持 iPhone
【发布时间】:2011-04-20 18:41:42
【问题描述】:

我有一个通过UITabBarController 控制的多个视图的情况。其中一个视图是通过UINavigationController 控制的。该应用程序应该只支持所有视图的肖像......除了一个单独的视图。此视图被推送到导航控制器的堆栈中,并且应该允许纵向和横向。

我尝试了所有我能想到的解决方案,但要么解决方案不起作用,要么更糟糕的是完全无法预测。

有人用干净的方式解决了这个问题吗?

【问题讨论】:

  • 您是否尝试过使用 UIDeviceOrientationDidChangeNotification 接收 UIDeviceOrientation 更改并做出相应反应(旋转变换)?如果您列出您想到的所有解决方案以及它们为什么不适合您,这会有所帮助(我想您确实尝试了各种 shouldAutorotateToInterfaceOrientation 组合)。
  • 顺便说一句-您要旋转的视图是否由视图控制器管理? UIViews 不会自己旋转,它们的控制器会旋转它们。见:stackoverflow.com/questions/4324394/…
  • @magma 是的,它们在控制器中。问题显然是基于 UITabBarController 的使用。

标签: iphone objective-c cocoa-touch ios orientation


【解决方案1】:

您使用presentModalViewControllerpushViewController 呈现的视图控制器应支持任何方向,使用此方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

使用此方法,当您以横向显示控制器时,它也将正确地以横向显示。

【讨论】:

  • 最终只是将视图重新实现为模态视图。与 tabbarcontroller 搞混最终成为一个无处可去的兔子洞。
【解决方案2】:

我遇到了同样的问题。您必须为您的UITabBarController 实现shouldAutorotateToInterfaceOrientation:,并为您希望在所有视图控制器中支持的所有方向返回 YES。并且在所有其他视图控制器的相同方法中,仅针对您希望在每个视图控制器中支持的方向返回 YES。

要为您的 UITabBarController 实现 shouldAutorotateToInterfaceOrientation:,您可以继承 UITabBarController,但更简单的方法是在 UITabBarController 上实现一个类别并只实现该方法:

@implementation UITabBarController(orientation)

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation
{
    return YES; // or whatever you need
}

@end

注意:在网络浏览器中输入;未编译。 :-)

您可以在最后将其放入您的应用委托 .m 文件中。

我发现 UITabBarController 只为肖像返回 YES,这显然也会影响所有从属视图控制器。就我而言,我有一个从属视图控制器,我想同时支持纵向和横向,这解决了它。

【讨论】:

  • 啊,这就是我的问题的核心。我创建了一个自定义的 UITabBarController 并覆盖了 shouldAutorotateToInterfaceOrientation 以传递可见视图控制器的值。但是,仍然存在一个问题,即在切换回另一个视图时,如果设备处于横向状态,它不会强制使用纵向。
【解决方案3】:

如果您在标签栏内有导航,那么唯一的选择是将特定视图呈现为模式视图(标签栏要求所有视图都支持自动旋转的方向)。所以基本上需要景观的单视图控制器应该实现

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
      if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
          // Create the landscape version of the view and present it modally
      }

      return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

然后在模态视图控制器的相同方法中关闭模态视图,通过检测纵向方向并使用父[self.parentViewController dismissModalViewControllerAnimated: animated]的方法

【讨论】:

    【解决方案4】:

    对于iOS-6,我已经这样做了,它运行得很好

    (NSUInteger)supportedInterfaceOrientations
    { 
        return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
    }
    
    (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    { 
        return UIInterfaceOrientationLandscapeLeft;
    }
    

    这会对你有所帮助....

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多