Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIDevice currentDevice's "orientation" always null

As per the title. Calling [[UIDevice currentDevice] BeginGeneratingDeviceOrientationNotifications] has no effect.

DidRotateToInterfaceOrientation etc events are working fine, but I need to be able to poll the device orientation arbitrarily.

How can I fix/do this?

The long story: I have a tab application with a navigation controller on each tab. The root view of tab number one is a graph that goes full screen when the orientation changes to landscape; however this needs to be checked whenever the view appears as the orientation change could have occurred elsewhere, so I was hoping to poll the orientation state whenever this view appears.

like image 347
Tobster Avatar asked Mar 17 '10 10:03

Tobster


4 Answers

UIDevice's notion of orientation seems to be only available on actual devices. The simulator seems to always return 0 here, regardless of whether the notifications have been enabled as the docs suggest. Irritatingly inconvenient, but there you go.

I find this works fine on the actual device:

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    NSLog(@"orientation: %d", [[UIDevice currentDevice] orientation]);
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
like image 51
Ben Zotto Avatar answered Sep 21 '22 14:09

Ben Zotto


seems like a silly question, but isn't it

beginGeneratingDeviceOrientationNotifications

( lower case b ) ...

like image 26
DavidG Avatar answered Sep 24 '22 14:09

DavidG


If you check [[UIDevice currentDevice] orientation] in - (void)viewDidLoad you will always get nil.

Check it in *- (void) viewDidAppear:(BOOL)*animated method and you

like image 20
PavelTheMan Avatar answered Sep 22 '22 14:09

PavelTheMan


this is as per iMeMyself said in the comments above - this samed me a lot of time and I think is the right answer so I wanted to highlight it here:

UIDeviceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;

if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
{
   //do stuff here
}
else if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
{
//or do stuff here
}
like image 34
craigk Avatar answered Sep 25 '22 14:09

craigk