【问题标题】:Screen rotation doesn't work on iOS 5 but is working on iOS 6屏幕旋转在 iOS 5 上不起作用,但在 iOS 6 上有效
【发布时间】:2013-07-22 15:59:20
【问题描述】:

我目前在 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];
}

LoyaltyListController.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 的屏幕:

肖像:

横向:(没有旋转)

它应该是这样的:

有什么想法吗?

【问题讨论】:

    标签: objective-c ios5 screen-orientation screen-rotation


    【解决方案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. */
    }
    

    【讨论】:

    • 你检查我的源代码了吗???这正是我正在使用的......- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationMaskAllButUpsideDown); }
    • 哦,我完全错过了。再检查一遍!不要使用 UIInterfaceOrientationMaskAllButUpsideDown。您只需根据 interfaceOrientaion 的值返回 YES 或 NO。你将不得不使用 ||看看你想支持哪个。在这种情况下,由于您想支持除颠倒之外的所有内容,因此我以这种方式编写了代码。
    • 没错,没看到UIInterfaceOrientationMaskAllButUpsideDown 是iOS6 及更高版本:) 我现在就测试一下
    【解决方案2】:

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

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

    【讨论】:

    • 你检查我的源代码了吗???这正是我正在使用的......- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationMaskAllButUpsideDown); }
    猜你喜欢
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    相关资源
    最近更新 更多