shouldAutorotateToInterfaceOrientation不起作用

18

我一直在以竖屏模式编写我的通用应用程序,现在已经有了大约15个Nib文件和许多视图控制器,我想实现shouldAutorotateToInterfaceOrientation方法并在横向模式下设计一些屏幕。

添加:

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

对于我的所有视图控制器,这个方法都没有起作用。

在调试期间,我看到这个方法被调用了,但它就是不工作!无论是在模拟器中还是在设备上,无论是在iPhone上还是在iPad上都不行!

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

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

还是不行,

加上:

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

 [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

将它们分别放在我的viewDidLoad和viewDidUnload中也没有起作用。

我很迷茫...任何帮助都可以!

还有一个信息... 我的所有视图都是UIControl类型,因为我需要TouchUpInside起作用。

非常感谢您的帮助。


你在哪些设备和/或模拟器版本上看到这个问题?你的根导航或选项卡控制器是什么类型? - warrenm
Xcode 3.2.2,iphone模拟器3.1.3(iPad 3.2),同时使用设备3.1.3。我在我的mainWindow Nib文件中拥有Tab Bar和Navigation Controller,并且所有的类都是UIViewController的子类。 - Dror Sabbag
5个回答

44

确保所有父视图的autoresizesSubviews属性设置为YES。如果您没有在Interface Builder中为所有视图设置springs和struts,则可能需要在代码中执行此操作。

引用Interface Builder用户指南:

  

重要提示:在Cocoa nib文件中,如果您未在Interface Builder中为视图设置任何springs或struts但随后使用setAutoresizingMask:方法在运行时添加自动调整大小的行为,则您的视图可能仍然不会表现出正确的自动调整大小行为。原因是,如果这些子视图没有设置springs和struts,则Interface Builder完全禁用父视图的子项的自动调整大小功能。要再次启用自动调整大小行为,您必须向父视图的setAutoresizesSubviews:方法传递YES。这样做后,子视图应正确自动调整大小。

还有几件事需要注意:

  1. 只有将根视图控制器也设置为autorotate时,UINavigationController才会旋转。

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


4
如果UITabBarController的所有视图控制器都设置为自动旋转,它才能自动旋转。这是一种错误的实现方式,希望苹果有朝一日能够解决这个问题。 - Adam
+1 这将简明扼要地向任何使用 nav- 和 tabbarcontrollers 制作“普通应用程序”的人解释自定义行为 - 对我来说,它很有效。另外需要注意的是,在 IB 中使用 navcontrollers 构建的带有 tabbar 的应用程序将获得正确的 rootcontroller,并且在创建 viewcontrollers 时默认的 struts、autosize 和视图方向设置都是正确的,因此无需设置或更正任何内容。 - Henrik Erlandsson
“只有当UITabBarController的所有视图控制器都设置为自动旋转时,它才会自动旋转。”(需要重复几遍)。似乎在iOS 6及以上版本中可能不是这种情况,但在iOS 5中是这样的(行为非常令人困惑)。 - Tim Arnold

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

1
你正在构建哪个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 将不允许屏幕更改方向。


0

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


0

我遇到了这个问题,但在iOS6中它可以工作,而在iOS5中却不行。结果发现我在storyboard中有一个视图,但还没有为其创建一个viewcontroller类。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接