[UIDevice currentDevice].orientation == UIDeviceOrientationUnknown following UINavigationController push
Asked Answered
S

1

7

This is an observation more than anything, because I appear to have found a work-around...

The code below fails to work when it's in a controller which has been pushed onto a UINavigationController stack. In this situation [UIDevice currentDevice].orientation consistently returns UIDeviceOrientationUnknown.

-(void)layoutAccordingToOrientation {
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    NSLog(@"layoutAccordingToOrientation/orientation==%i", orientation);
    if (UIInterfaceOrientationIsLandscape(orientation)) {
        :
        _detailToolbar.frame = CGRectMake(0, 660, 1024, 44);
    } else {
        :
        _detailToolbar.frame = CGRectMake(0, 916, 768, 44);
    }
}

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [self layoutAccordingToOrientation];
}

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

The following call has been made:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

To work around this UIDeviceOrientationUnknown-problem, I now use the following instead:

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsLandscape(orientation)) {
    etc.
} else {
    etc.
}

... which works every time.

Still, I fail to see why the first variation would not work in the context of a pushed view controller. Ideas anyone? Is it simply a bug?

Slipperwort answered 13/12, 2011 at 8:4 Comment(1)
I am having the same problem, however I am not able to get your work around to work in my situation either.Misinform
C
4

I could think of a couple of things here, but I'm not sure they're what you're looking for...

First, when the app is launched, usually the device orientation returns UIDeviceOrientationUnknown for performance reasons. One thing you could try is to layout your subviews inside a .nib or in your Storyboard, with the correct autoresizing options, and let iOS do its thing.

Secondly, if you're like me, maybe you're testing on a device that's right beside your computer, laying on a table. Well, in that case, you are not going to get the UIDeviceOrientationUnknown thing. You should actually test for UIDeviceOrientationIsValidInterfaceOrientation([[UIDevice currentDevice] orientation]). If the device is laying down on its back, for example, it returns yes to that statement. This also means that when you use UIInterfaceOrientationIsLandscape(orientation), you're getting the right result for the wrong reason. The UIInterfaceOrientationIsLandscape is returning false not because the device's orientation is UIDeviceOrientationPortrait, but because it's invalid.

Not so sure if this helped, but it took me a while to figure that out and I thought it would be nice to share this info! :)

Chilpancingo answered 16/8, 2012 at 22:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.