【问题标题】:iOS 6 supportedInterfaceOrientations issueiOS 6 支持的接口方向问题
【发布时间】:2012-12-16 14:56:03
【问题描述】:

在我的视图控制器中,我实现了两种控制界面方向的方法:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate
{
    return YES;
}

supportedInterfaceOrientations方法中,我返回UIInterfaceOrientationMaskPortrait,但当时我意识到shouldAutorotate方法没有被调用。

但我在supportedInterfaceOrientations 方法中更改为return UIInterfaceOrientationPortrait。正在调用shouldAutorotate 方法,但出现以下错误:

UIApplicationInvalidInterfaceOrientation, reason: '支持的方向与应用程序没有共同的方向,并且 shouldAutorotate 返回 YES'

顺便说一下,我在支持的界面方向中选择了所有方向。

已编辑

我使用 viewController 并嵌入了 navigationController。 这里是 AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate,UINavigationControllerDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (readonly, strong, nonatomic) UINavigationController *navController;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
@end

在 AppDelegate.m 下的 didFinishLaunchingWithOptions 方法中

      navController = (UINavigationController *)self.window.rootViewController;
                IPad_HomeViewController *rootVC=(IPad_HomeViewController *)navController.topViewController;
                rootVC.managedObjectContext = self.managedObjectContext;
return YES;

在我的 iPad_HomeViewController 中,

@interface IPad_HomeViewController : UIViewController <UINavigationControllerDelegate>
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@end

【问题讨论】:

  • 我不认为代码只需要改变它的方向,你可以在菜单 Targets->Summary-> Supported Device Orientations 中执行此操作并更改如下。
  • @Programmer... 如果特定屏幕支持与更广泛的应用程序不同的方向,您必须在代码中执行此操作。
  • 检查我对 ios 6 和 pre ios 6 示例代码的回答。希望对您有所帮助。
  • 您的应用委托看起来不错。尝试在所有视图控制器中将 autorotate 设置为 yes,然后设置所需的旋转。看看这是否有效。我实际上构建了一个演示单视图项目并测试了代码。它工作正常。
  • 我在 shouldAutorotate 方法中返回 Yes 并在 supportInterfaceOrientations 方法中设置 UIInterfaceOrientationMaskPortrait。但是 shouldAutorotate 没有被调用并且错误仍然存​​在。顺便说一句,我也有 ios5 的 shouldAutorotateToInterfaceOrientation 方法。如果可能的话,你能把你的示例代码发给我吗?真的谢谢。

标签: objective-c ios cocoa-touch orientation


【解决方案1】:
- (BOOL) shouldAutorotate {
    return YES;
}

// for landscape
- (NSInteger) supportedInterfaceOrientations {
    return (1 << UIInterfaceOrientationLandscapeLeft) | 
           (1 << UIInterfaceOrientationLandscapeRight);
}

如上所述,查看特定方向的掩码值:UIInterfaceOrientationMask 值。UIInterfaceOrientationMask

【讨论】:

    【解决方案2】:

    两点:

    1. 您的错误消息完全有道理,因为使用UIInterfaceOrientationPortrait 作为来自supportedInterfaceOrientations 的返回值是没有意义的,它返回的是位掩码。使用UIInterfaceOrientationMask 值之一。

    2. 您似乎担心如果您使用正确的UIInterfaceOrientationMaskPortrait,iOS 似乎不会调用shouldAutorotate。如果考虑到物理设备的物理方向和应用程序的当前方向与supportedInterfaceOrientations 相比,可能需要旋转,它可能只会调用shouldAutorotate。如果它断定设备已经处于可接受的方向,为什么还要检查shouldAutorotate

    请参阅supportedInterfaceOrientationsHandling View Rotations 了解更多信息。

    【讨论】:

    • 实际上我遇到了一些问题。我使用了故事板并创建了很多视图控制器。我希望 viewController A 进入纵向模式,即使用户旋转 ipad 设备,它仍处于纵向模式。此外,我想要 viewControllerB 的横向(左)模式。当用户从 viewController A 切换到 viewController B 时,viewControllerB 会自动更改为横向(左)模式并锁定为横向,即使用户更改为纵向模式也是如此。
    【解决方案3】:

    我知道这听起来很简单,但我在 iPhone 上测试时绞尽脑汁想弄清楚方向问题 - 我在纵向模式下设置了物理自动锁定模式 - 所以我在编程上所做的任何更改都无关紧要 - 认为这应该是故障排除第 1 步!

    【讨论】:

      【解决方案4】:

      要更改方向设置,请选择菜单 Targets->Summary-> Supported Device Orientations 并进行如下更改。

      如果方向按钮是黑色的,则表示您已将其选为方向之一。

      【讨论】:

        【解决方案5】:

        而不是使用整数来声明方向为什么不使用 bool 并在其中插入 if 语句来检测您想要的任何方向。这是一个示例代码,希望对您有所帮助:

        - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
        {
        if (interfaceOrientation == UIInterfaceOrientationPortrait) {
            return YES;
        }
        if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
            return YES;
        }
        return NO;
        }
        

        您可以在 if 语句中添加所有方向,它应该可以正常工作。阿德里安

        编辑:

        如果你想为 ios 6 提供一个选项,下面的代码应该适合你。

        - (BOOL) shouldAutorotate
        {
        return YES;
         }
        
        -(NSUInteger)supportedInterfaceOrientations
        {
        return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft   | UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
        }
        

        你可以在 ios 6 中更改几乎所有支持的方向。快乐编码

        编辑 1:

        要以某种方式仅旋转某些viewcontrollers,只需使用我在上面发布的所有viewcontrollers 的ios 6 代码。步骤如下:

        1. 在所有四个internfaceorientations 所在的项目级别中,继续前进并关闭所有内容,以便应用程序恢复默认设置。

        2. 实现我在所有viewcontrollers中提供的ios 6代码。

        3. 而不是在shouldautorotate 方法中声明否。
        4. 在第二种方法中,插入您想要的任何类型的方向。

        这应该可以为您解决问题。快乐编码

        【讨论】:

        • 谢谢抢。好点子。我为 ios 6 添加了更多代码到我的答案中。再次感谢。
        • 实际上我遇到了一些问题。我使用了故事板并创建了很多视图控制器。我希望 viewController A 进入纵向模式,即使用户旋转 ipad 设备,它仍处于纵向模式。此外,我想要 viewControllerB 的横向(左)模式。当用户从 viewController A 切换到 viewController B 时,viewControllerB 会自动更改为横向(左)模式并锁定为横向,即使用户更改为纵向模式也是如此。
        • 这很容易飞镖。请检查我的答案的编辑。
        • Hi Amp,我是按照你的方法做的。我关闭了项目级别的每个方向。但仅检测supportedInterfaceOrientations 方法并且仍然不调用shouldAutorotate。可能是我设置错误的 AppDelegate。我编辑了我的问题。
        • 我评论了您的编辑。您的问题是否已解决或仍然存在?
        猜你喜欢
        • 2012-07-24
        • 2012-09-30
        • 1970-01-01
        • 2013-05-04
        • 1970-01-01
        • 2012-09-12
        • 1970-01-01
        • 1970-01-01
        • 2013-01-03
        相关资源
        最近更新 更多