【问题标题】:shouldAutorotateToInterfaceOrientation doesn't workshouldAutorotateToInterfaceOrientation 不起作用
【发布时间】:2011-02-21 12:38:01
【问题描述】:

我一直在以纵向模式编写我的通用应用程序, 现在经过大约 15 个 nib 文件,许多 viewCotnrollers, 我想实现 shouldAutorotateToInterfaceOrientation 并在横向模式下设计一些屏幕。

添加:

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

对我所有的 viewControllers,不做这项工作。

在调试期间,我看到此方法被调用,但它不起作用!不在模拟器中,不在设备中,不在 Iphone 中,不在 Ipad 中!

我在论坛中搜索了一些答案,并看到了一些使用建议:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 return (interfaceOrientation == UIInterfaceOrientationPortrait ||
   interfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
   interfaceOrientation == UIInterfaceOrientationLandscapeRight ||
   interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown );
} 

也没有用,

添加:

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

 [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

分别对我的 viewDidLoad 和 viewDidUnload 也不起作用。

我迷路了..任何帮助都可以!

还有一个信息...我所有的视图都是 UIControl 类型,因为我需要 TuchUpInside 才能工作。

感谢您的帮助。

【问题讨论】:

  • 您在哪些设备和/或模拟器版本上看到了这个?您的根导航或标签栏控制器的类型是什么?
  • Xcode 3.2.2, iphone 模拟器 3.1.3, (ipad 3.2) 以及使用设备 3.1.3。我的 mainWindow Nib 文件中有选项卡栏和导航控制器。但我所有的类都是 UIViewController 的子类

标签: iphone xcode ipad uiviewcontroller screen-orientation


【解决方案1】:

确保您所有的父视图都具有 autoresizesSubviews = YES。如果您尚未在 IB 中为所有视图设置弹簧和支柱,则可能需要在代码中执行此操作。

引用 Interface Builder 用户指南:

重要提示:在 Cocoa nib 文件中,如果您 不要设置任何弹簧或支柱 您在 Interface Builder 中的视图,但是 然后使用 setAutoresizingMask: 添加自动调整大小行为的方法 运行时,您的视图可能仍然没有 展示正确的自动调整大小 行为。原因是接口 生成器禁用自动调整大小 父视图的孩子完全如果 那些孩子没有弹簧, 支柱设置。启用自动调整大小 再次行为,您必须将 YES 传递给 setAutoresizesSubviews: 方法 父视图。这样做后, 子视图应该正确地自动调整大小。

需要注意的其他几件事:

  1. 只有当它的根视图控制器也设置为自动旋转时,UINavigationController 才会自动旋转。

  2. 只有当所有视图控制器都设置为自动旋转时,UITabBarController 才会自动旋转。

【讨论】:

  • "UITabBarController 只有在其所有视图控制器都设置为自动旋转时才会自动旋转。" --- 每次都得到我;这只是 Apple 实现 API 的错误方式。希望他们有一天能修复它。
  • +1 这将简明扼要地向使用导航和标签栏控制器制作“普通应用程序”的任何人解释自定义行为 - 它对我有用并且有效。附带说明一下,在 IB 的选项卡栏中使用导航控制器构建的应用程序将获得正确的根控制器,并且在创建视图控制器时默认的 struts、自动调整大小和视图方向设置都很好,因此无需设置或更正其中任何一个。
  • "只有当它的所有视图控制器都设置为自动旋转时,UITabBarController 才会自动旋转。" !!!! (熊重复:)。似乎对于 iOS 6 及更高版本可能不是这种情况,但对于 iOS 5 来说是这样(对这种行为感到非常困惑)。
【解决方案2】:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    UIInterfaceOrientation des=self.interfaceOrientation;

    if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) //iPad
    {
        if(des==UIInterfaceOrientationPortrait||des==UIInterfaceOrientationPortraitUpsideDown)//ipad-portairait
        {

        }
        else//ipad -landscape
        {

        }
    }
    else//iphone
    {
        UIInterfaceOrientation des=self.interfaceOrientation;

        if(des==UIInterfaceOrientationPortrait||des==UIInterfaceOrientationPortraitUpsideDown) //iphone portrait
        {

        }
        else //iphone -landscape
        {

        }
    }
return YES;
}

【讨论】:

    【解决方案3】:

    我遇到了这个问题,但它在 iOS6 中有效,但在 iOS5 中无效。结果我的故事板中有一个视图,我还没有创建视图控制器类。

    【讨论】:

      【解决方案4】:

      ...最后但并非最不重要的一点是,确保您没有在测试设备上激活“人像方向锁定”设置(当然不适用于模拟器),这将禁用任何应用程序中的旋转 no不管shouldAutorotateToInterfaceOrientation 返回什么。

      【讨论】:

        【解决方案5】:

        您正在为哪个 iOS 构建?它在 iOS 6.0 中已被弃用。 (您应该改写supportedInterfaceOrientations 和preferredInterfaceOrientationForPresentation 方法。)

        你也可以在 UIViewController 类上调用 shouldAutorotate:

        https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/shouldAutorotate


        确保您已在项目设置的“摘要”选项卡中检查了支持的界面方向(在最顶部的“项目导航器”中选择项目名称)。

        如果这里没有选择你想要使用的方向,那么模拟器/iphone将不允许屏幕改变方向。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-10
          • 1970-01-01
          • 2011-02-20
          • 2012-10-25
          相关资源
          最近更新 更多