1

我目前在 iOS 5 上遇到屏幕旋转问题。在 iOS 6 上,一切正常,但旋转在 iOS 5 上不起作用,即使UIDeviceOrientationDidChangeNotification调用了通知。

我正在使用Storyboard,这就是我ViewController的样子:

在此处输入图像描述

为了旋转屏幕,我使用以下方法:

CustomTabBarViewController.mm ( UITabBarController)

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    if (isLandscapeOK) {
        // for iPhone, you could also return UIInterfaceOrientationMaskAllButUpsideDown
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (isLandscapeOK) {
        // for iPhone, you could also return UIInterfaceOrientationMaskAllButUpsideDown
        return (interfaceOrientation == UIInterfaceOrientationMaskAllButUpsideDown);
    }
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

TabBarItemController.mm ( UINavigationView)

-(BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return [self.topViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

忠诚度列表控制器.mm ( UIViewController)

- (void) didRotate:(NSNotification *)notification
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

    if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight)
    {
        self.navigationController.navigationBarHidden = YES;
        [self hideTabBar:self.tabBarController];
        portraitView.hidden = YES;
        landscapeView.hidden = NO;
    }
    else if (orientation == UIDeviceOrientationPortrait)
    {
        self.navigationController.navigationBarHidden = NO;
        [self showTabBar:self.tabBarController];
        portraitView.hidden = NO;
        landscapeView.hidden = YES;
        [notificationView setFrame:CGRectMake(0, - (notificationView.frame.size.height + 40), notificationView.frame.size.width, notificationView.frame.size.height)];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
    [(CustomTabBarViewController*)[self tabBarController] setLandscapeOK:YES];
}

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

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

这是 iOS 5 的屏幕:

肖像:

在此处输入图像描述

横向:(未旋转)

在此处输入图像描述

它应该是这样的:

在此处输入图像描述

任何想法?

4

2 回答 2

1

对于 iOS 5 使用- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)

在 iOS 6 中,它已被弃用,但如果你使用它也没关系。只有在 iOS 5.0 或更低版本的情况下才会调用它。

更新UIInterfaceOrientationMaskAllButUpsideDown不是 UIInterfaceOrientation 枚举类型。遮罩在 iOS 6 中用于提供遮罩值,以提供您想要支持的方向组合,但在 iOS 5 中。

您将必须检查 interfaceOrientaion 是否是

UIInterfaceOrientationLandscapeLeft,UIInterfaceOrientationLandscapeRight, UIInterfaceOrientationPortrait or UIInterfaceOrientationPortraightUpsideDown.

并根据您要支持的所有方向返回“是”或“否”。

在你的情况下使用:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
   return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
 /* If you are supporting all, simply return YES,

 if you aren't supporting any return NO, otherwise Use || and check which ones you wanna return YES for.
 In this case you wanna support all except UpsideDown hence use this. */
}
于 2013-07-22T16:05:43.883 回答
-1

您必须返回 YES 以支持所有方向 iOS 5 和以前的版本以及它在 iOS 中已弃用并且在 iOS 6 中不被调用,您已经正确实现了 iOS6 方法

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}
于 2013-07-22T16:06:21.153 回答